按键盘 ACTION_DONE 关闭 Android 首选项对话框

发布于 2024-08-24 21:03:48 字数 638 浏览 5 评论 0原文

我希望能够关闭编辑首选项对话框(如下所示 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 中实现相同的效果。

I would like to be able to close the editpreference dialog (as shown here http://twitpic.com/18ttdp) by pressing the 'Done' button on the keyboard.

Currently, pressing 'Done' just dismisses the keyboard but leaves the dialog.

In other parts of my application I use code similar to the following to intercept the 'Done' key press and execute actions in my activity:

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

However, I am unsure of how to do achieve this same effect in my preference activity or layout xml.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼睛会笑 2024-08-31 21:03:48

您应该执行类似以下操作,而不是在那里添加侦听器:

getDialog().setOnKeyListener(new OnKeyListener() {

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

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

Instead of adding the listener there, you should do something similar to this:

getDialog().setOnKeyListener(new OnKeyListener() {

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

This code will dismiss the dialog when a key is pressed.

方觉久 2024-08-31 21:03:48

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

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

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

I had the same problem, this is how I solved it:

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

For my needs, the input was a number (hence the commented out line) but if you want text use what's there.

通知家属抬走 2024-08-31 21:03:48

以下是我解决这个问题的方法:

    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,并且它的实现将来可能会改变。

Here is how I solved it:

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

But you may want to consider subclassing EditTextPreference instead, since this onClick call is a hack, and its implementation may change in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文