android:按下完成键时软键盘执行操作

发布于 2024-12-06 20:06:30 字数 76 浏览 0 评论 0原文

我有一个编辑文本。我希望在输入一些文本后,当用户按下软键盘的“完成”键时,它应该直接执行一些搜索操作,我也在按钮单击事件中实现了这些操作。

I have an EditText. I want that after typing some text, when user presses the Done key of the softkeyboard, it should directly perform some search operation which I have also implemented in a button click event.

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

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

发布评论

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

评论(5

夜巴黎 2024-12-13 20:06:30

试试这个

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

Try this

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});
树深时见影 2024-12-13 20:06:30

试试这个

它对于DONERETURN都有效。

EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                            || actionId == EditorInfo.IME_ACTION_DONE) {
                        // Do your action
                        return true;
                    }
                    return false;
                }
            });

Try this

It works both for DONE and RETURN.

EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                            || actionId == EditorInfo.IME_ACTION_DONE) {
                        // Do your action
                        return true;
                    }
                    return false;
                }
            });
短暂陪伴 2024-12-13 20:06:30

您捕获KeyEvent,然后检查其键码。 FLAG_EDITOR_ACTION 用于识别来自输入键已自动标记为“next”或“done”的 IME 的输入键

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

查找文档 此处

第二种方法

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    int result = actionId & EditorInfo.IME_MASK_ACTION;
    switch(result) {
    case EditorInfo.IME_ACTION_DONE:
        // done stuff
        break;
    case EditorInfo.IME_ACTION_NEXT:
        // next stuff
        break;
    }
 }
});

You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

Find the docs here.

Second Method

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    int result = actionId & EditorInfo.IME_MASK_ACTION;
    switch(result) {
    case EditorInfo.IME_ACTION_DONE:
        // done stuff
        break;
    case EditorInfo.IME_ACTION_NEXT:
        // next stuff
        break;
    }
 }
});
很酷不放纵 2024-12-13 20:06:30

尝试一下

,无论您的键盘显示输入符号还是下一个箭头符号,这都适用于两种情况,

YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
            {
                //Perform Action here 
            }
            return false;
        }
    });

如果您面对红线,则执行此操作...
按 alt+enter 导入 Keyevent 和 EditorInfo
然后所有错误删除它就会正确......

Try this

This will work in both condition whether your keyboard is showing enter sign or next arrow sign

YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
            {
                //Perform Action here 
            }
            return false;
        }
    });

if u r facing red line then do this...
import Keyevent and EditorInfo by pressing alt+enter
then all the errors remove it will properly.......

春夜浅 2024-12-13 20:06:30

在 Kotlin 中使用

viewBinding.editText.setOnEditorActionListener { view, actionId, event ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        //react to action
    }
    false
}

您还可以在“if”大括号内返回 true 来消费事件 - 这样当您按下完成时键盘不会按下

In Kotlin use

viewBinding.editText.setOnEditorActionListener { view, actionId, event ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        //react to action
    }
    false
}

You can also return true inside 'if' braces to consume event - this way keyboard won't go down when you press done

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