91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Android中使用EditText實現批量搜索關鍵詞

發布時間:2020-11-24 17:16:32 來源:億速云 閱讀:311 作者:Leah 欄目:移動開發

如何在Android中使用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

<&#63;xml version="1.0" encoding="utf-8"&#63;> 
<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

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

看完上述內容,你們掌握如何在Android中使用EditText實現批量搜索關鍵詞的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

孙吴县| 兴山县| 黔西县| 独山县| 辉南县| 峨眉山市| 湟源县| 来凤县| 高台县| 武平县| 介休市| 泗洪县| 柳江县| 诸暨市| 黎城县| 丽江市| 左权县| 敦煌市| 绥芬河市| 体育| 淮阳县| 乳源| 新密市| 尼木县| 阿拉尔市| 杭锦后旗| 南平市| 中西区| 华坪县| 当雄县| 澎湖县| 临夏县| 周至县| 札达县| 英德市| 株洲市| 东兰县| 赤壁市| 安泽县| 平陆县| 上林县|