开发者

How to catch key events while soft keyboard is open android?

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... an开发者_如何学Goy way to catch the event anyway?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜