android-第一次单击时不显示Datepicker
作者:互联网
我有一个编辑字段,当我单击它时,将打开一个日期选择器,但是第一次单击时我无法使该日期选择器弹出.如果我第一次单击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);
}
}
解决方法:
在您的布局xml中,在您的edittext-
android:focusableInTouchMode="false"
这会工作
标签:datepickerdialog,android 来源: https://codeday.me/bug/20191028/1951756.html