-1

这是我的代码,可以正常工作 updare edittext 但问题是 edittext 不会立即更新,直到我再次单击 edittext 告诉我如何更新 datepicker dailog 上的 edittext(正按钮) h 日期选择器显示两个默认按钮正负我如何更新请在积极按钮上编辑文本帮助我

    EditText edtKeyword;
    String selectedDate;
int mYear,mMonth,mDay ;

    DateFormat fmtDateAndTime = DateFormat.getDateInstance();
Calendar myCalendar = Calendar.getInstance();

DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    myCalendar.set(Calendar.YEAR, year);
    myCalendar.set(Calendar.MONTH, monthOfYear);
    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        edtKeyword = (EditText) findViewById(R.id.edtKeyword);

        edtKeyword.setClickable(true);

        edtKeyword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(MenuList.this, d, myCalendar
                    .get(Calendar.YEAR),                   
                     myCalendar.get(Calendar.MONTH),
         myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                mYear = myCalendar.get(Calendar.YEAR);
                mMonth = myCalendar.get(Calendar.MONTH);
                mDay = myCalendar.get(Calendar.DAY_OF_MONTH);

                selectedDate= mMonth+"-"+mDay+"-"+mYear;

                edtKeyword.setText(selectedDate);               
        }
    });        

    edtKeyword.setText(fmtDateAndTime.format(myCalendar.getTime()));
4

1 回答 1

0

我试过这段代码。这对我来说可以。请尝试一次

private int pYear = 1970;
private int pMonth = 1;
private int pDay = 1;
static final int DATE_DIALOG_ID = 0;

然后在 onCreate 中:

    edt_birth.setKeyListener(null);
    edt_birth.setOnTouchListener(new OnTouchListener() {
        @SuppressWarnings("deprecation")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });


@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
                pDay);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        pYear = year;
        pMonth = monthOfYear;
        pDay = dayOfMonth;
        updateDisplay();
    }
};

private void updateDisplay() {
    int temp = pMonth + 1;
    edt_birth.setText(pDay + "/" + temp + "/" + pYear);
    }

这将更新您的编辑文本。

于 2013-07-16T10:56:08.987 回答