相信很多同行,都使用过android 自带的日期或者时间控件,在api 14(记不清是哪个版本)以上的不会有这个错误,ui是直接拖动的,但14以下的版本是类似于这样的:
使用这个控件,就会出现这样的问题,点击红框内,系统会自动弹出软键盘,本来是挺好的功能,可惜,输入进去的数字,点击确定提交之后是无效的,这势必影响了使用的功能,我们就需要禁止让用户点击输入框,不能弹出软键盘,解决的方式是参考如下国外网站的:
里面提供了两种方式:
1.禁止日期控件获得焦点,测试通过,如下代码:
datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
2.第二种看不太懂,直接贴出来,还没测试过:
DatePicker m_dtPicker = (DatePicker) findViewById(R.id.dt_picker);
setDisabledTextViews(m_dtPicker);
private void setDisabledTextViews(ViewGroup dp)
{
for (int x = 0, n = dp.getChildCount(); x < n; x++)
{
View v = dp.getChildAt(x);
if (v instanceof TextView)
{
v.setEnabled(false);
}
else if (v instanceof ViewGroup)
{
setDisabledTextViews((ViewGroup)v);
}
}
}
/**
* 弹出日期时间选择框方法
*
* @return
*/
public AlertDialog dateTimePicKDialog() {
LinearLayout dateTimeLayout = (LinearLayout) activity
.getLayoutInflater().inflate(R.layout.common_datetime, null);
datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);
timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);
datePicker
.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
timePicker
.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
init(datePicker, timePicker);
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(this);
ad = new AlertDialog.Builder(activity)
.setTitle(initDateTime)
.setView(dateTimeLayout)
.setPositiveButton("設置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
Date date = null;
try {
date = format.parse(dateTime);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
}
}).setCancelable(false).show();
onDateChanged(null, 0, 0, 0);
return ad;
}