EditText在手机弹出的软键盘中,将回车键变为搜索键和点击搜索键时,执行2次搜索解决

在手机弹出的软键盘中,回车键变为搜索键。

1、修改EditText属性:

<EditText  
  android:id="@+id/et_search"  
  android:layout_width="100dp"  
  android:layout_height="25dp"  
  android:textSize="12sp"  
  android:hint="请输入关键词"  
  android:imeOptions="actionSearch"  
  android:singleLine="true"/>  
  
  android:imeOption="actionSearch"的作用是将回车两字改为搜索,
android:singleLine="true"的作用是防止搜索框换行。

2、 点击时执行两次监听事件的问题:


每次点击软键盘的搜索键都会执行两次搜索方法,
没有加event.getAction() == KeyEvent.ACTION_DOWN这句判断。
修改代码如下:
OnKeyListener事件:
et_search=(EditText)findViewById(R.id.et_search);  
et_search.setOnKeyListener(new View.OnKeyListener() {  
   @Override  
   public boolean onKey(View v, int keyCode, KeyEvent event) {  
       //是否是回车键  
       if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {  
           //隐藏键盘  
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))  
 .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()  
 .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
           //搜索  
           search();  
       }  
       return false;  
   }  
});  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值