开发者

Event when user select back?

How can I catch the event when the user, after he entered the text on an EditText, select Back and the开发者_运维问答 keyboard disappear?


Override onBackPressed at your Activity class:

    @Override
    public void onBackPressed() {
        //If keyboard is on, do what you want
        super.onBackPressed();
    }


If you want to catch the back key when user has finished entering text in EditText and he presses back key then you should use:

EditText edit = (EditText)findViewById(R.id.yourId);
    edit.setOnKeyListener(new EditText.OnKeyListener(){
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_BACK){
                    System.out.println("******back key caught in edit.setOnKeyListener");
                }
                    return false;
                }

            });


I had the same problem. Vineet Shukla's answer was not working, until I made sure that the delegate was a EditText.OnKeyListener. Prior to that it was a View.OnKeyListener and I was not seeing KeyEvent.KEYCODE_BACK == keyCode, for was I even seeing onKey ever called. I hope this is helpful to someone having a similar problem, although this post is a year old. Cheers.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜