我有一個editfield,當我點擊它時,日期選擇器會打開,但我無法在第一次點擊時彈出日期選擇器彈出。如果我第一次點擊編輯區域,日期選擇器不會出現。當我點擊第二或第三次時,它正在顯示。這裏是我的代碼:日期選擇器第一次點擊時沒有顯示
public class MainActivity extends Activity implements OnClickListener {
private Calendar calendar;
private int day;
private int month;
private int year;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
date = (EditText) findViewById(R.id.date);
calendar = Calendar.getInstance();
day = calendar.get(Calendar.DAY_OF_MONTH);
month = calendar.get(Calendar.MONTH);
year = calendar.get(Calendar.YEAR);
date.setOnClickListener(this);
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
date.setText(selectedYear + " - " + (selectedMonth + 1) + " - " + selectedDay);
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(0);
}
}
的ShowDialog()方法已很長一段時間了,請考慮使用DilaogFragment代替 – Stepango