Android文本框搜索和清空效果實(shí)現(xiàn)代碼及簡要概述
本文實(shí)現(xiàn)的效果:文本框輸入為空時(shí)顯示輸入的圖標(biāo);不為空時(shí)顯示清空的圖標(biāo),此時(shí)點(diǎn)擊清空圖標(biāo)能清空文本框內(nèi)輸入文字。
正文
一、實(shí)現(xiàn)效果


二、實(shí)現(xiàn)代碼
綁定事件
private Drawable mIconSearchDefault; // 搜索文本框默認(rèn)圖標(biāo)
private Drawable mIconSearchClear; // 搜索文本框清除文本內(nèi)容圖標(biāo)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main)
final Resources res = getResources();
mIconSearchDefault = res.getDrawable(R.drawable.txt_search_default);
mIconSearchClear = res.getDrawable(R.drawable.txt_search_clear);
mSearchView = (EditText) findViewById(R.id.txtSearch);
mSearchView.addTextChangedListener(tbxSearch_TextChanged);
mSearchView.setOnTouchListener(txtSearch_OnTouch);
}
觸摸事件
private OnTouchListener txtSearch_OnTouch = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
int curX = (int) event.getX();
if (curX > v.getWidth() - 38
&& !TextUtils.isEmpty(mSearchView.getText())) {
mSearchView.setText("");
int cacheInputType = mSearchView.getInputType();// backup the input type
mSearchView.setInputType(InputType.TYPE_NULL);// disable soft input
mSearchView.onTouchEvent(event);// call native handler
mSearchView.setInputType(cacheInputType);// restore input type
return true;// consume touch even
}
break;
}
return false;
}
};
//監(jiān)聽輸入
/**
* 動(dòng)態(tài)搜索
*/
private TextWatcher tbxSearch_TextChanged = new TextWatcher() {
//緩存上一次文本框內(nèi)是否為空
private boolean isnull = true;
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s)) {
if (!isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
null, mIconSearchDefault, null);
isnull = true;
}
} else {
if (isnull) {
mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,
null, mIconSearchClear, null);
isnull = false;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
/**
* 隨著文本框內(nèi)容改變動(dòng)態(tài)改變列表內(nèi)容
*/
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
};
代碼說明:
1.為輸入框綁定觸摸事件(模擬點(diǎn)擊事件捕捉)。通過監(jiān)聽點(diǎn)擊區(qū)域判斷是否點(diǎn)擊清空圖片,如果在該區(qū)域并且文本框不為空,則清空文本框。
2.為輸入框綁定文本改變事件監(jiān)聽,根據(jù)內(nèi)容改變動(dòng)態(tài)設(shè)置圖標(biāo)顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android SearchView搜索框組件的使用方法
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- Android 百度地圖POI搜索功能實(shí)例代碼
- android實(shí)現(xiàn)讀取、搜索聯(lián)系人的代碼
- Android撥號(hào)盤 支持T9搜索和號(hào)碼搜索等撥號(hào)盤案例
- Android搜索框通用版
- Android實(shí)現(xiàn)帶列表的地圖POI周邊搜索功能
- Android搜索框組件SearchView的基本使用方法
- Android實(shí)現(xiàn)搜索本地音樂的方法
相關(guān)文章
揭秘雙十一手機(jī)淘寶圖標(biāo)如何被動(dòng)態(tài)更換
這篇文章主要介紹了每到雙十一十二的時(shí)候Android手機(jī)動(dòng)態(tài)更換手機(jī)圖標(biāo)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Java和Android的LRU緩存及實(shí)現(xiàn)原理
本文主要介紹 Java和Android的LRU緩存及實(shí)現(xiàn)原理,這里整理了詳細(xì)的資料,有興趣的小伙伴可以參考下便于學(xué)習(xí)理解2016-08-08ijkPlayer播放器的全自動(dòng)編譯腳本及最終編譯包
這篇文章主要介紹了ijkPlayer播放器的全自動(dòng)編譯腳本及最終編譯包,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android點(diǎn)擊Button實(shí)現(xiàn)功能的幾種方法總結(jié)
當(dāng)Button有多個(gè)或者Button的使用次數(shù)很多時(shí),我們需要采用綁定監(jiān)聽器的做法,其實(shí),綁定監(jiān)聽器也有幾種方法,不過,我在這里就不一一列舉了,畢竟那些方法在實(shí)際的應(yīng)用中也不常見2013-10-10Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯(cuò)誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11Android實(shí)現(xiàn)Gesture手勢識(shí)別用法分析
這篇文章主要介紹了Android實(shí)現(xiàn)Gesture手勢識(shí)別用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android基于Gesture實(shí)現(xiàn)手勢識(shí)別的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-09-09Android Loader的使用以及手機(jī)通訊錄的獲取方法
下面小編就為大家分享一篇Android Loader的使用以及手機(jī)通訊錄的獲取方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android實(shí)現(xiàn)檢測實(shí)體按鍵事件并屏蔽
這篇文章主要介紹了Android實(shí)現(xiàn)檢測實(shí)體按鍵事件并屏蔽 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Kotlin 創(chuàng)建接口或者抽象類的匿名對象實(shí)例
這篇文章主要介紹了Kotlin 創(chuàng)建接口或者抽象類的匿名對象實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03