如何在Android中使用EditText實(shí)現(xiàn)批量搜索關(guān)鍵詞?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問題。
十載的慶城網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整慶城建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“慶城網(wǎng)站設(shè)計(jì)”,“慶城網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
當(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)容,你們掌握如何在Android中使用EditText實(shí)現(xiàn)批量搜索關(guān)鍵詞的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁(yè)標(biāo)題:如何在Android中使用EditText實(shí)現(xiàn)批量搜索關(guān)鍵詞
文章網(wǎng)址:http://chinadenli.net/article12/giijdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、虛擬主機(jī)、服務(wù)器托管、企業(yè)建站、網(wǎng)站制作、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)