Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AlertDialog中的NumberPicker始终激活键盘。如何禁用此功能?_Android_Dialog_Android Widget_Keyboard_Android Softkeyboard - Fatal编程技术网

Android AlertDialog中的NumberPicker始终激活键盘。如何禁用此功能?

Android AlertDialog中的NumberPicker始终激活键盘。如何禁用此功能?,android,dialog,android-widget,keyboard,android-softkeyboard,Android,Dialog,Android Widget,Keyboard,Android Softkeyboard,对所有人来说 我正在尝试让一个简单的NumberPicker在AlertDialog中工作。问题是,每当我增加/减少numberpicker中的值时,键盘就会激活 有许多帖子描述了这个问题,但没有一条建议有效。 我试过: android:configChanges="keyboard|keyboardHidden" 及 及 在初始化之前和之后(dialog.show()),在按键事件(显然使用侦听器)上,我都尝试过调用这些函数,但到目前为止没有成功 完整代码: popup.xml <?x

对所有人来说

我正在尝试让一个简单的NumberPicker在AlertDialog中工作。问题是,每当我增加/减少numberpicker中的值时,键盘就会激活

有许多帖子描述了这个问题,但没有一条建议有效。 我试过:

android:configChanges="keyboard|keyboardHidden"

在初始化之前和之后(dialog.show()),在按键事件(显然使用侦听器)上,我都尝试过调用这些函数,但到目前为止没有成功

完整代码:

popup.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myNumber"
        android:configChanges="keyboard|keyboardHidden"   
    />
</RelativeLayout>
谢谢你的帮助


Barry

android:configChanges属性属于您的
Manifest.xml
文件,而不是布局。但是如果你把键盘藏在那里,你可能是最好的选择

android:windowSoftInputMode="stateAlwaysHidden"

您还应该尝试在
活动
上使用
getWindow()
,而不仅仅是
对话框
,看看这是否有帮助。这可能是最好的解决方案,因为第一个解决方案(从清单中隐藏)将在整个活动中隐藏键盘。

因为无法访问NumberPicker按钮,所以“不可能”这样做

我做了一个快速肮脏的黑客来处理它

首先将焦点生成器添加到布局中,在本例中为大小为0的按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myNumber"
        android:configChanges="keyboard|keyboardHidden"
        />
    <Button
        android:id="@+id/myBtn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        />
</RelativeLayout>
这不是推荐的解决方案。我希望有人能找到更好的。
仅用于教育目的;-)

事实上,尽管上述解决方案非常有效,但还有一种更简单的方法

picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
这就是所需要的一切。无论如何,谢谢你的回复!!!!:-)

XML:

<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />


Hello Jave,感谢您的回答以及您对清单与布局XML的见解。然而,问题是,我不能将这些参数添加到布局中,因为布局基本上是一个表单。numberpicker是这个表单上的一个弹出窗口(alertdialog),我只需要禁用这个对话框上的键盘,而不是整个活动。顺便说一句,这是您已经看到的:-)活动的getWindow是我已经尝试过的,不幸的是没有帮助您也可以在XML中设置它:android:DegenantFocusability=“BlocksDescents”
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
    <NumberPicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myNumber"
        android:configChanges="keyboard|keyboardHidden"
        />
    <Button
        android:id="@+id/myBtn"
        android:layout_width="0dp"
        android:layout_height="0dp"
        />
</RelativeLayout>
    EditText edit = null;
    try {
        final Field[] fields = picker.getClass().getDeclaredFields();
        for (Field f : fields) {
            if (EditText.class.equals(f.getType())) {
                f.setAccessible(true);
                edit = (EditText) f.get(picker);
            }
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    if (edit != null) {
        final EditText finalEdit = edit;
        final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                dialog.findViewById(R.id.myBtn).requestFocusFromTouch();
                imm.hideSoftInputFromWindow(finalEdit.getWindowToken(), 0);
            }
        });
    }
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
<NumberPicker
    ...
    android:descendantFocusability="blocksDescendants" />