这是我的代码,工作正常updare edittext但问题是edittext不立即更新,直到我再次点击edittext告诉我如何更新datepicker dailog上的edittext(正面按钮)h日期选择器显示两个默认按钮正面和负面如何我在正面按钮上更新editteext请帮助我
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()));
答案 0 :(得分: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);
}
这将更新您的编辑文字。