zoukankan      html  css  js  c++  java
  • Android 监听软键盘搜索键

      现在很多的Android应用都有了数据搜索功能,在以往的设计上,会使用搜索框+搜索按钮来实现搜索功能; 现在呢,越来越流行的是,去除搜索按钮,直接监听软键盘搜索键,当用户输入完搜索关键字后,直接点击软件盘上的“所搜”键,查询要搜索等信息。


      要实现上面的搜索效果,当然,我们要监听软键盘的搜索键。

    EditText mEditSearch = (EditText)this.findViewById(R.id.mEditSearch);
    
    mEditSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
    
                        String keytag = mEditSearch.getText().toString().trim();
    
                        if (TextUtil.isEmpty(keytag)) {
                            Toast.makeText(PlanSearchActivity.this, "请输入搜索关键字", Toast.LENGTH_SHORT).show();
    
                            return false;
                        }
    
    					// 搜索功能主体
    
                        return true;
                    }
                    return false;
                }
            });
    


    当然,只有这样还不够,还需要设置EdtiText属性:

    <EditText
                android:id="@+id/editSearch"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="6dp"
                android:layout_marginBottom="6dp"
                android:background="#FFFFFF"
                android:gravity="center_vertical"
                android:hint="输入关键字"
                android:imeOptions="actionSearch"
                android:paddingBottom="0dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="0dp"
                android:singleLine="true"
                android:textColor="#555555"
                android:textColorHint="#AAAAAA"
                android:textSize="14sp" />

    有两个地方需要设置android:imeOptions="actionSearch"及android:singleLine="true";如果不设置这两个属性,你将无法看到搜索键(软键盘默认显示的键是“确定”)。

    如此这般,就OK啦!欢迎互相学习!
    如有疑问,欢迎进QQ群:487786925( Android研发村 )

  • 相关阅读:
    让开发效率“飞起”的VS Code 插件
    转-webpack学习笔记--整体配置结构
    十二、vue中watch原理
    十一、vue生命周期诠释--带图
    十、vue mixins 的用法
    八、Web移动端Fixed布局的解决方案
    七、vue中v-for有时候对页面不会重新渲染,数组变化后如何到渲染页面
    六、vue如何缓存页面
    五、vue常用UI组件
    vue组件递归
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329938.html
Copyright © 2011-2022 走看看