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研发村 )

  • 相关阅读:
    移动端测试知识概览
    24、CSS定位
    23、Xpath
    MySQL触发器
    MySQL存储过程和函数
    Cookie详解
    简单漏桶限流
    PHP异常和错误
    工厂方法模式
    简单工厂模式
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329938.html
Copyright © 2011-2022 走看看