1

我希望能够通过按下键盘上的“完成”按钮来关闭 editpreference 对话框(如此处所示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 中实现同样的效果。

4

3 回答 3

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

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

于 2011-03-19T13:02:48.673 回答
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,并且它的实现将来可能会改变。

于 2014-07-03T23:49:31.007 回答
0

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

getDialog().setOnKeyListener(new OnKeyListener() {

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

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

于 2010-03-16T03:24:11.213 回答