首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >AlertDialog中的NumberPicker总是激活键盘。如何禁用此功能?

AlertDialog中的NumberPicker总是激活键盘。如何禁用此功能?
EN

Stack Overflow用户
提问于 2012-06-12 20:42:35
回答 4查看 7.5K关注 0票数 17

致所有人:

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

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

代码语言:javascript
复制
android:configChanges="keyboard|keyboardHidden"

代码语言:javascript
复制
inputManager.hideSoftInputFromWindow(currentView.getWindowToken(), 0);

代码语言:javascript
复制
getWindow().setSoftInputMode(
     WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

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

完整的代码:

popup.xml

代码语言:javascript
复制
<?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>

和调用函数:

代码语言:javascript
复制
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();      

感谢您的任何帮助

巴里

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-06-13 01:56:17

实际上,尽管上面的解决方案工作得很好,但还有一种更简单的方法。

代码语言:javascript
复制
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);

这就是所需要的一切。无论如何,感谢您的回复!:-)

票数 25
EN

Stack Overflow用户

发布于 2012-06-12 22:50:13

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

我做了一个又脏又脏的黑客来处理它。

首先向布局添加焦点食客,在本例中为0大小的Button:

代码语言:javascript
复制
<?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,找不到更好的东西。

代码语言:javascript
复制
    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
EN

Stack Overflow用户

发布于 2012-06-12 21:00:16

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

代码语言:javascript
复制
android:windowSoftInputMode="stateAlwaysHidden"

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10996880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档