Android EditText實(shí)現(xiàn)關(guān)鍵詞批量搜索示例
今天在項(xiàng)目中用到了用到了一種特殊的EditText,當(dāng)用戶在EditText中輸入內(nèi)容,點(diǎn)擊搜索按鈕的時(shí)候,輸入的內(nèi)容能夠高亮,然后添加到輸入的容器中。刪除的時(shí)候,能夠?qū)⑷萜髦械年P(guān)鍵詞逐一刪除。附上代碼:
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android ListView用EditText實(shí)現(xiàn)搜索功能效果
- Android 根據(jù)EditText搜索框ListView動(dòng)態(tài)顯示數(shù)據(jù)
- Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中
- Android中搜索圖標(biāo)和文字居中的EditText實(shí)例
- Android控件系列之EditText使用方法
- Android定制自己的EditText輕松改變底線顏色
- Android中EditText顯示明文與密碼的兩種方式
- Android更改EditText下劃線顏色樣式的方法
- Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
- Android利用EditText如何實(shí)現(xiàn)搜索框詳解
相關(guān)文章
五分了解Android?Progress?Bar進(jìn)度條加載
這篇文章主要為大家介紹了Android?Progress?Bar進(jìn)度條加載的實(shí)現(xiàn)及屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android MotionEvent中g(shù)etX()和getRawX()的區(qū)別實(shí)例詳解
這篇文章主要介紹了Android MotionEvent中g(shù)etX()和getRawX()的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Android自定義TextView實(shí)現(xiàn)drawableLeft內(nèi)容居中
這篇文章主要介紹了Android自定義TextView實(shí)現(xiàn)drawableLeft內(nèi)容居中的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android自定義Seekbar滑動(dòng)條 Pop提示跟隨滑動(dòng)按鈕滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android自定義Seekbar滑動(dòng)條,Pop提示跟隨滑動(dòng)按鈕滑動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android逆向之dex2oat的實(shí)現(xiàn)解析
虛擬機(jī)的發(fā)生展經(jīng)歷了從初期的dalvik,到中期的dalvik,以及后期的ART。但是市面上的APK文件早已已經(jīng)全球流行。為了能夠讓這些APK不加改動(dòng)的在所有虛擬機(jī)上面運(yùn)行,google采用了類似適配器模式。即在讓虛擬運(yùn)行之前多一道工序。就是dexopt2021-10-10

