两个不同的onClick为DatePicker的不同对话框

时间:2016-07-31 13:37:26

标签: android datepicker

我为reservationreturn设置了两个不同的edittext,我希望它有一个不同的日期选择器,但是当我在使用方法时,它会说Method does not override method from its super class这里是我的代码。

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);
        }
    };


}

感谢帮助人员。 :)

1 个答案:

答案 0 :(得分: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;
}

<强>更新

另一个错误在于:

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

两个对话框都具有相同的ID。您必须设置DIALOG_ID1 = 1;