欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-17 09:44:28
今天在项目中用到了用到了一种特殊的edittext,当用户在edittext中输入内容,点击搜索按钮的时候,输入的内容能够高亮,然后添加到输入的容器中。删除的时候,能够将容...

今天在项目中用到了用到了一种特殊的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> 

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

上一篇:

下一篇: