在屏幕上打开DatePickerDialog时如何隐藏软键盘 [英] how to hide the softketboard when DatePickerDialog is open on the screen

查看:414
本文介绍了在屏幕上打开DatePickerDialog时如何隐藏软键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自定义DatePickerDialog,以为本地DatePicker添加一些功能.如预期般运作.但是默认情况下,DatepickerDialog的日,月和年字段是可编辑的.因此,当我关注那些可编辑的输入字段时,默认情况下将打开软键盘,使用该软键盘将能够编辑日期/月/年,并且当用户编辑日期/月/年时,按set/cancel编辑的日期DatePickerDialog将关闭,并且功能正常运行.在这里,我的问题是当用户编辑日期/月份/年份并按设置/取消编辑的日期DatePickerDialog时,而软键盘仍处于打开状态.一旦关闭DatePickerDialog,它就不会关闭.因此,我尝试使用以下代码隐藏软键盘.

I am customizing DatePickerDialog to add some of my functionality for native DatePicker. that is working as expected. But day, month and year fields of DatepickerDialog are editable by default. so when i focus those editable input fields soft keyboard will be open by default and used will be able to edit the date/month/year and when user edit the day/month/year ans press set/cancel edited date DatePickerDialog is getting closed and functionality is working properly. Here my issue is when user edit the day/month/year ans press set/cancel edited date DatePickerDialog is getting closed and softkeyboard is still opened. its not getting closed as soon as DatePickerDialog is dismissed. so for that i tried to hide the softkeyboard with the following code.

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

    }
};

但是这不起作用.因此,我尝试使用以下代码切换软键盘.

But this is not working. So I tried to toggle the softkeyboard with the following code.

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.

But here imm.isActive() is always returning false. if i remove that if(imm.isActive()) condition its working when softkeyboard is open. but when softkeyboard is not opened and used just opened DatePickerDialog and set or cancel the date by changing with arrows softkeyboard is getting opened on dismiss of DatePickerDialog. So i need to get that imm.isActive() value properly. but its always returning false.

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

So some one please help me to get the proper way to decide whether softkeyboard is opened or not.

推荐答案

这是停止datepickerdialog中手动编辑的最佳选择.您可以使用此功能禁用软键盘.

This is the best option which stops manual editing in datepickerdialog. You can disable the softkeyboard using this.

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

这篇关于在屏幕上打开DatePickerDialog时如何隐藏软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆