Android EditText通过imeOptions属性控制软键盘的回车键变化:回车,搜索,下一个,完成等,并响应事件

1.在Layout文件中,给EditText文件添加属性android:imeOptions=""

属性值含义:

actionUnspecified:未指定

actionNone :没有动作

actionSearch: 搜索

actionSend: 发送

actionNext: 下一项

actionGo:去往

actionDone :完成

对应用到的常量:

EditorInfo.IME_ACTION_UNSPECIFIED

EditorInfo.IME_ACTION_NONE

EditorInfo.IME_ACTION_SEARCH

EditorInfo.IME_ACTION_SEND

EditorInfo.IME_ACTION_NEXT

EditorInfo.IME_ACTION_GO

EditorInfo.IME_ACTION_DONE

 

2.EditText添加监听器

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 

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

       if (actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) 

          //do something;    

      {                     

     return true;             

   }               

    return false;           

}       

});

 

注意事项:

1.版本高于2.3,EditText设置inputType为text或singleLine为true才会有效果

2.onEditorAction需返回true,不然方法会执行两次

你可能感兴趣的:(Android,EditText)