【问题标题】:how to hide the softketboard when DatePickerDialog is open on the screen如何在屏幕上打开 DatePickerDialog 时隐藏软键盘
【发布时间】:2012-12-10 07:24:57
【问题描述】:

我正在自定义 DatePickerDialog 来为原生 DatePicker 添加一些我的功能。这是按预期工作的。但是默认情况下 DatepickerDialog 的日、月和年字段是可编辑的。所以当我关注那些可编辑的输入字段时,软键盘将默认打开并使用将能够编辑日期/月/年,当用户编辑日/月/年并按下设置/取消编辑日期时 DatePickerDialog 正在关闭和功能正常工作。 这里我的问题是当用户编辑日/月/年并按下设置/取消编辑日期 DatePickerDialog 正在关闭并且软键盘仍然打开时。一旦 DatePickerDialog 被解雇,它就不会关闭。所以为此我尝试使用以下代码隐藏软键盘。

 private DialogInterface.OnDismissListener myOnDismissListener = new  DialogInterface.OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
        InputMethodManager imm = (InputMethodManager) ((Activity) ctContext)
                .getSystemService(Activity.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(this.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);

    }
};

但这不起作用。所以我尝试使用以下代码切换软键盘。

private DialogInterface.OnDismissListener myOnDismissListener = new DialogInterface.OnDismissListener() {
    public void onDismiss(DialogInterface dialog) {
        InputMethodManager imm = (InputMethodManager) ((Activity) ctContext)
                .getSystemService(Activity.INPUT_METHOD_SERVICE);
        if(imm.isActive())
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
};

但是这里 imm.isActive() 总是返回 false。如果我删除了 if(imm.isActive()) 条件它在软键盘打开时的工作。但是当软键盘没有打开并使用时,只需打开 DatePickerDialog 并通过使用箭头更改来设置或取消日期软键盘在关闭 DatePickerDialog 时打开。所以我需要正确获取 imm.isActive() 值。但它总是返回 false。

所以请有人帮我找到正确的方法来决定是否打开软键盘。

【问题讨论】:

    标签: android android-softkeyboard android-datepicker datepickerdialog


    【解决方案1】:

    这是在 datepickerdialog 中停止手动编辑的最佳选项。您可以使用它禁用软键盘。

             final DatePickerDialog dp = new DatePickerDialog(MainActivity.this, new    DatePickerDialog.OnDateSetListener() {
    
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    
                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, monthOfYear, dayOfMonth);
                        txtdate.setText(dateFormatter.format(newDate.getTime()));
    
                    }
                }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
    
                dp.show();
                dp.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    

    【讨论】:

      【解决方案2】:

      这是我处理强制关闭软键盘的方式

      @Override
      public void dismiss() {
          super.dismiss();
          //hide the soft keyboard
          getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
      }
      

      【讨论】:

        【解决方案3】:

        DatePickerDialog 项中停止手动编辑的最佳选项是使用设置 descendantFocusability 。您可以在代码或对话框样式文件中添加它。我个人建议在风格上使用它。

        方法一:使用代码

        datePickerDialog.datePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS)
        

        方法2:使用样式

        <style name="MyDatePickerDialogTheme" parent="android:Theme.Material.Light.Dialog"> <item name="android:descendantFocusability">blocksDescendants</item </style>

        现在在对话框中设置样式

        datePickerDialog = DatePickerDialog(
                            this.context,
                            R.style.MyDatePickerDialogTheme
                        )
        

        我想你明白了。快乐的代码。

        【讨论】:

          猜你喜欢
          • 2015-10-26
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          • 2023-01-09
          • 2018-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多