2016-07-31 79 views
0

我有reservationreturn兩個不同的edittext,我希望它有不同的日期選取器,但是當我騎着一個方法時它說Method does not override method from its super class這是我的代碼。兩種不同的onClick爲不同的DatePicker對話框

public class TransactionActivity extends AppCompatActivity { 

    EditText etResDate, etReturnDate ; 
    Button btnRent; 
    int year_x,month_x,day_x; 
    int year_x1,month_x1,day_x1; 
    int hour_x,minute_x; 

    static final int DIALOG_ID = 0; 
    static final int DIALOG_ID1 = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_transaction); 
     showDialogOnButtonClick(); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     final Calendar cal = Calendar.getInstance(); 
     year_x = cal.get(Calendar.YEAR); 
     month_x = cal.get(Calendar.MONTH); 
     day_x = cal.get(Calendar.DAY_OF_MONTH); 
     year_x1 = cal.get(Calendar.YEAR); 
     month_x1 = cal.get(Calendar.MONTH); 
     day_x1 = cal.get(Calendar.DAY_OF_MONTH); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 


     etResDate = (EditText) findViewById(R.id.etResDate); 
     etReturnDate = (EditText) findViewById(R.id.etReturnDate); 
    } 

    public void showDialogOnButtonClick() { 
     etResDate = (EditText) findViewById(R.id.etResDate); 
     etReturnDate = (EditText) findViewById(R.id.etReturnDate); 

     etResDate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showDialog(DIALOG_ID); 
      } 
     }); 

     etReturnDate.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showDialog(DIALOG_ID1); 
      } 
     }); 

    } 

    @Override 
    protected Dialog onCreateDialog(int id){ 
     if(id == DIALOG_ID){ 
      return new DatePickerDialog(this, dplistener, year_x,month_x,day_x); 
     } 
     else if (id == DIALOG_ID1){ 
      return new DatePickerDialog(this, dplistener1, year_x1, month_x1, day_x1); 
     } 
     return null; 
    } 

    private DatePickerDialog.OnDateSetListener dplistener = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      year_x = year; 
      month_x = monthOfYear + 1; 
      day_x = dayOfMonth; 
      etResDate.setText(month_x + "/" + day_x + "/" + year_x); 
     } 

    }; 
    private DatePickerDialog.OnDateSetListener dplistener1 = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int year1, int monthOfYear1, int dayOfMonth1) { 
      year_x1 = year1; 
      month_x1 = monthOfYear1 + 1; 
      day_x1 = dayOfMonth1; 
      etReturnDate.setText(month_x1 + "/" + day_x1 + "/" + year_x1); 
     } 
    }; 


} 

感謝您的幫助球員。 :)

回答

0

我想這是正確和onCreateDialog()onCreateDialog1()

也許,你只需要刪除onCreateDialog1()和更新onCreateDialog()如下:

@Override 
protected Dialog onCreateDialog(int id){ 
    if(id == DIALOG_ID){ 
     return new DatePickerDialog(this, dplistener, year_x,month_x,day_x); 
    else if (id == DIALOG_ID1) { 
     return new DatePickerDialog(this, dplistener1, year_x, month_x, day_x); 
    } 
    return null; 
} 

UPDATE

另一個錯誤在這裏:

static final int DIALOG_ID = 0; 
static final int DIALOG_ID1 = 0; 

兩個對話框都有相同的ID。您必須設置DIALOG_ID1 = 1;

+0

謝謝你先生!它讓技巧!你是最好的! :)再次謝謝:) –

+0

@ B.Dee我很高興我能幫助你。最好的祝福! – W0rmH0le

相關問題