【问题标题】:NumberPicker in AlertDialog always activates keyboard. How to disable this?AlertDialog 中的 NumberPicker 始终激活键盘。如何禁用此功能?
【发布时间】:2012-06-15 08:21:36
【问题描述】:

致各位

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

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

android:configChanges="keyboard|keyboardHidden"

inputManager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);

还有

getWindow().setSoftInputMode(
     WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

我曾尝试在初始化之前和之后调用这些函数(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>

以及调用函数:

AlertDialog.Builder builder =  new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        return;
    } }); 
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        return;
    } }); 
View view = getLayoutInflater().inflate(R.layout.popup, null);
builder.setView (view);

final AlertDialog dialog = builder.create ();
NumberPicker picker = (NumberPicker) view.findViewById(R.id.myNumber);
picker.setMinValue(0);
picker.setMaxValue(999);

dialog.getWindow().
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
dialog.show();      

任何帮助表示赞赏

巴里

【问题讨论】:

    标签: android dialog android-widget keyboard android-softkeyboard


    【解决方案1】:

    实际上,虽然上面的解决方案完美无缺,但还有一种更简单的方法。

    picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    

    这就是我们所需要的。无论如何,谢谢你的回复!!!! :-)

    【讨论】:

    • 你也可以在 XML 中设置:android:descendantFocusability="blocksDescendants"
    【解决方案2】:

    由于无法访问 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>
    

    我们需要捕捉点击递增/递减按钮的事件,我选择了 OnValueChangedListener,找不到更好的。

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

    这不是推荐的解决方案。我希望有人会找到更好的东西。 仅用于教育目的;-)

    【讨论】:

      【解决方案3】:

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

      android:windowSoftInputMode="stateAlwaysHidden"
      

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

      【讨论】:

      • 您好 Jave,感谢您的回答以及您对 Manifest 与布局 xmls 的见解。然而,问题是我无法将这些参数添加到布局中,因为布局基本上是一个表单。 numberpicker 是这个表单上的一个弹出窗口(alertdialog),我只需要禁用这个对话框上的键盘,而不是整个活动。顺便说一句,这就是你已经提到的 :-) 活动上的 getWindow 是我已经尝试过的,但不幸的是没有帮助
      【解决方案4】:

      XML:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-14
        • 2015-11-06
        • 2015-06-12
        • 2015-03-27
        • 1970-01-01
        • 2016-10-11
        • 1970-01-01
        • 2018-06-14
        相关资源
        最近更新 更多