android DatePicker 点击文本框,禁止弹出键盘

         相信很多同行,都使用过android 自带的日期或者时间控件,在api 14(记不清是哪个版本)以上的不会有这个错误,ui是直接拖动的,但14以下的版本是类似于这样的:



       使用这个控件,就会出现这样的问题,点击红框内,系统会自动弹出软键盘,本来是挺好的功能,可惜,输入进去的数字,点击确定提交之后是无效的,这势必影响了使用的功能,我们就需要禁止让用户点击输入框,不能弹出软键盘,解决的方式是参考如下国外网站的:

 android datepicker禁止弹出软键盘

    里面提供了两种方式:

  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;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值