在登录页面中,用户必须输入他的生日。我想创建一个 EditText(生日),然后 DatePicker 出现。
我喜欢这样:
public class Main extends Activity {
EditText inputBirthDay;
DatePicker dpResult;
TextView tvDisplayDate;
int year;
int month;
int day;
static final int DATE_DIALOG_ID = 999;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_activity);
inputBirthDay = (EditText) findViewById(R.id.registerBirthDay);
inputBirthDay.setFocusableInTouchMode(false);
addListenerOnButton();
}
public void addListenerOnButton() {
inputBirthDay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
showDialog(DATE_DIALOG_ID);
inputBirthDay.requestFocus();
}
});
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
inputBirthDay.setText(new StringBuilder().append(day)
.append("-").append(month + 1).append("- ").append(year)
.append(" "));
}
};
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
}
但我有一个问题:
1/当我点击EditText时,键盘第一次出现,然后当我再次点击时,DatePicker出现。如何隐藏键盘?
谢谢您的帮助