Android EditText实现关键词批量搜索示例

所属分类: 软件编程 / Android 阅读数: 42
收藏 0 赞 0 分享

今天在项目中用到了用到了一种特殊的EditText,当用户在EditText中输入内容,点击搜索按钮的时候,输入的内容能够高亮,然后添加到输入的容器中。删除的时候,能够将容器中的关键词逐一删除。附上代码:

SearchEditText.java

package com.jackie.searchresultedittext; 
 
import android.content.Context; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
 
/** 
 * Created by Jackie on 2017/2/21. 
 * 用于搜索的EditText 
 */ 
public class SearchEditText extends RelativeLayout { 
  private Context mContext; 
  private LayoutInflater mInflater; 
  private View mView; 
  private LinearLayout mContainer; 
  private EditText mEditText = null; 
 
  public SearchEditText(Context context) { 
    this(context, null); 
  } 
 
  public SearchEditText(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
  } 
 
  public SearchEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context); 
  } 
   
  private OnSearchChangeListener mSearchChangeListener; 
 
  public interface OnSearchChangeListener { 
    void searchChange(String s); 
    void removeView(int position); 
  } 
 
  public void setOnSearchChangeListener(OnSearchChangeListener searchChangeListener) { 
    mSearchChangeListener = searchChangeListener; 
  } 
 
  private void init(Context context) { 
    mContext = context; 
    mInflater = LayoutInflater.from(mContext); 
    mView = mInflater.inflate(R.layout.search_edittext_layout, null); 
    LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    params.leftMargin = 15; 
    params.rightMargin = 15; 
    addView(mView, params); 
     
    mContainer = (LinearLayout) mView.findViewById(R.id.layout); 
    mEditText = (EditText) mView.findViewById(R.id.edittext); 
    mEditText.setOnKeyListener(new OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_DEL) { 
          if (isNotFastClick()) { 
            if (mEditText.getText().toString().length() > 0) { 
              String str = mEditText.getText().toString(); 
              str = str.substring(0, str.length() - 1); 
              mEditText.setText(str); 
              mEditText.setSelection(str.length()); 
            } else { 
              if (mContainer.getChildCount() > 0) { 
                if (mSearchChangeListener != null) { 
                  mSearchChangeListener.removeView(mContainer.getChildCount() - 1); 
                } 
 
                mContainer.removeViewAt(mContainer.getChildCount() - 1); 
              } 
            } 
          } 
        } 
 
        return true; 
      } 
    }); 
 
    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_SEARCH) { 
 
          if (mEditText.getText().toString().trim().equals("")) { 
            return true; 
          } 
 
          TextView textView = new TextView(mContext); 
          textView.setText(mEditText.getText().toString().trim()); 
          textView.setTextSize(14); 
          textView.setTextColor(Color.parseColor("#dfe0e0")); 
          textView.setPadding(10, 0, 10, 0); 
          textView.setBackgroundResource(R.drawable.shape_edittext_round_bg); 
          textView.setGravity(Gravity.CENTER); 
          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
          params.leftMargin = 10; 
          textView.setLayoutParams(params); 
 
          if (mSearchChangeListener != null) { 
            mSearchChangeListener.searchChange(mEditText.getText().toString().trim()); 
          } 
 
          mEditText.setText(""); 
          mContainer.addView(textView); 
        } 
 
        return true; 
      } 
    }); 
  } 
 
  public EditText getEditText() { 
    return mEditText; 
  } 
  public LinearLayout getContainer() { 
    return mContainer; 
  } 
 
  long lastClickTime = 0; 
 
  public boolean isNotFastClick() { 
    long time = System.currentTimeMillis(); 
    if (time - lastClickTime >= 300) { 
      lastClickTime = time; 
      return true; 
    } else { 
      return false; 
    } 
  } 
} 

search_edittext_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:scrollbars="none" 
  android:fillViewport="true" 
  > 
  <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="33dp" 
    android:orientation="horizontal"> 
 
    <LinearLayout 
      android:id="@+id/layout" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:gravity="center_vertical" 
      android:layout_gravity="center_vertical" /> 
 
    <EditText 
      android:gravity="center_vertical" 
      android:layout_gravity="center_vertical" 
      android:id="@+id/edittext" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:background="@null" 
      android:textSize="16dp" 
      android:textColor="#dfe0e0" 
      android:layout_weight="1" 
      android:minWidth="50dp" 
      android:imeOptions="actionSearch" 
      android:singleLine="true" 
      android:layout_marginLeft="10dp"/> 
  </LinearLayout> 
</HorizontalScrollView> 

shape_edittext_round_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <solid android:color="#666666" /> 
  <corners android:radius="10dp"/> 
</shape> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多