您的位置:首页 > 其它

安卓监听软键盘按键

2017-11-09 15:54 106 查看
1。在布局中,给EditText添加 android:imeOptions,以下以监听actionNext为例,具体请自行调整

<EditText  

        android:id="@+id/edittext"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:singleLine="true"  

       android:imeOptions="actionNext"/>  

2。在代码中,给EditText设置监听事件

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {  

              

            @Override  

            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  

                /*判断是否是“NEXT”键*/  

                if(actionId == EditorInfo.IME_ACTION_NEXT){  

                    /*隐藏软键盘*/  

                    InputMethodManager imm = (InputMethodManager) v  

                            .getContext().getSystemService(  

                                    Context.INPUT_METHOD_SERVICE);  

                    if (imm.isActive()) {  

                        imm.hideSoftInputFromWindow(  

                                v.getApplicationWindowToken(), 0);  

                    }  

                      

                    edittext.setText("success");  

                    webview.loadUrl(URL);  

                      

                    return true;  

                }  

                return false;  

            }  

        });  

常用按键:

actionNone : 回车键,按下后光标到下一行

actionGo : Go,

actionSearch : 放大镜

actionSend : Send

actionNext : Next

actionDone : Done,确定/完成,隐藏软键盘,即使不是最后一个文本输入框
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: