android 软键盘打开时如何捕获按键事件?

发布于 2024-11-26 21:12:31 字数 154 浏览 2 评论 0原文

我知道如何通过重写来捕获活动中的关键事件

public boolean onKeyDown(int keyCode, KeyEvent event)

问题是当软键盘打开时甚至不会调用此方法... 有什么办法可以赶上这个事件吗?

I know how to catch key events in my activity by overriding

public boolean onKeyDown(int keyCode, KeyEvent event)

The problem is when the soft keyboard is open this method is not even called...
any way to catch the event anyway?

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

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

发布评论

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

评论(1

凝望流年 2024-12-03 21:12:31

是的,当我们谈论软键盘时,这意味着它不那么容易使用。顺便说一下,与软键盘相关的方法没有达到预期的结果。例如:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    //do something
    return super.onKeyDown(keyCode, event);
}

我发现的解决方法是使用 TextWatcher。将此代码用于您的应用程序

YourEdit = (EditText) findViewById(R.id.YourEdit);         
YourEdit.addTextChangedListener(new TextWatcher()
{
        public void  afterTextChanged (Editable s)
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "afterTextChanged", 3).show();    
            //do something
        } 

        public void  beforeTextChanged  (CharSequence s, int start, int count, int after)
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "beforeTextChanged", 3).show();    
            //do something
        } 

        public void  onTextChanged  (CharSequence s, int start, int before, int count) 
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "onTextChanged", 3).show();    
            //do something
        }
});

这些方法按顺序调用:beforeTextChanged、onTextChanged 和 afterTextChanged。您可以捕获文本更改的任何阶段。
祝你好运!

Yes, when we speak about soft keyboard it's means that no so easy to use it. By the way methods that relate to soft keyboard take no expected result. For example:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    //do something
    return super.onKeyDown(keyCode, event);
}

The workaround that I've found is using of TextWatcher. Use this code for your application

YourEdit = (EditText) findViewById(R.id.YourEdit);         
YourEdit.addTextChangedListener(new TextWatcher()
{
        public void  afterTextChanged (Editable s)
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "afterTextChanged", 3).show();    
            //do something
        } 

        public void  beforeTextChanged  (CharSequence s, int start, int count, int after)
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "beforeTextChanged", 3).show();    
            //do something
        } 

        public void  onTextChanged  (CharSequence s, int start, int before, int count) 
        { 
            //checking of TextWatcher functionality
            Toast.makeText(mContext, "onTextChanged", 3).show();    
            //do something
        }
});

These methods are called sequentially: beforeTextChanged, onTextChanged and afterTextChanged. You can catch any phase of text changing.
Good luck!

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