android 捕获Enter键的点击事件

捕获Enter键的点击事件

这个也很简单,只需为EditText设置事件监听:setOnEditorActionListener即可,然后

重写里面的onEditorAction(TextView v, int actionId, KeyEvent event)方法即可,

接着就根据你的需求,对应Enter键处于什么状态,就响应对应事件咯,很简单,示例代码如下:

注意是按下Enter键才会触发这个事件哦!

  1. public OnEditorActionListener editorActionListener = new OnEditorActionListener() {       
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         if(actionId == EditorInfo.IME_ACTION_UNSPECIFIED)  
  5.         {  
  6.             Toast.makeText(getApplicationContext(), "无效果", Toast.LENGTH_SHORT).show();  
  7.         }  
  8.         else if(actionId == EditorInfo.IME_ACTION_SEARCH)  
  9.         {  
  10.             Toast.makeText(getApplicationContext(), "查询", Toast.LENGTH_SHORT).show();  
  11.         }  
  12.         return true;  
  13.     }  
  14. };  
posted @ 2017-11-06 08:44  Mr.zzz  阅读(54)  评论(0编辑  收藏  举报