在键盘ACTION_DONE上按下解除Android首选项对话框

时间:2010-03-15 23:13:50

标签: android keyboard preferences

我希望能够通过按键盘上的“完成”按钮关闭编辑首选项对话框(如http://twitpic.com/18ttdp所示)。

目前,按'完成'只会解除键盘但离开对话框。

在我的应用程序的其他部分,我使用类似于以下的代码拦截“完成”键按下并执行我的活动中的操作:

text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                //do stuff here
                return true;
            }
            return false;
        }
    });

但是,我不确定如何在我的首选项活动或布局xml中实现相同的效果。

3 个答案:

答案 0 :(得分:0)

不是在那里添加监听器,而应该做类似的事情:

getDialog().setOnKeyListener(new OnKeyListener() {

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        dialog.dismiss();
        return true;
    }
});

当按下某个键时,此代码将关闭该对话框。

答案 1 :(得分:0)

我有同样的问题,这就是我解决它的方法:

    // edit text to get input
    final EditText input = new EditText(this);
    //input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
    alert.setView(input);

    // ok button
    alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do stuff
        }
    });

根据我的需要,输入是一个数字(因此是注释掉的行)但如果你想要文本使用那里的那些。

答案 2 :(得分:0)

以下是我解决它的方法:

    final EditTextPreference namePref = (EditTextPreference) findPreference("name");
    namePref.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_DONE) {
                namePref.onClick(null, DialogInterface.BUTTON_POSITIVE);
                namePref.getDialog().dismiss();
                return true;
            }
            return false;
        }
    });

但是你可能想要考虑继承EditTextPreference,因为这个onClick调用是一个hack,它的实现可能在将来发生变化。

相关问题