Android文本框搜索和清空效果實(shí)現(xiàn)代碼及簡(jiǎn)要概述
更新時(shí)間:2013年02月16日 16:24:57 作者:
在工作過(guò)程中可能會(huì)遇到這樣一個(gè)效果:文本框輸入為空時(shí)顯示輸入的圖標(biāo);不為空時(shí)顯示清空的圖標(biāo),此時(shí)點(diǎn)擊清空?qǐng)D標(biāo)能清空文本框內(nèi)輸入文字,感興趣的你可以了解下哦,或許對(duì)你學(xué)習(xí)android有所幫助
前言
本文實(shí)現(xiàn)的效果:文本框輸入為空時(shí)顯示輸入的圖標(biāo);不為空時(shí)顯示清空的圖標(biāo),此時(shí)點(diǎn)擊清空?qǐng)D標(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)聽(tīng)輸入
/**
* 動(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) {
}
};
代碼說(shuō)明:
1.為輸入框綁定觸摸事件(模擬點(diǎn)擊事件捕捉)。通過(guò)監(jiān)聽(tīng)點(diǎn)擊區(qū)域判斷是否點(diǎn)擊清空?qǐng)D片,如果在該區(qū)域并且文本框不為空,則清空文本框。
2.為輸入框綁定文本改變事件監(jiān)聽(tīng),根據(jù)內(nèi)容改變動(dòng)態(tài)設(shè)置圖標(biāo)顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
本文實(shí)現(xiàn)的效果:文本框輸入為空時(shí)顯示輸入的圖標(biāo);不為空時(shí)顯示清空的圖標(biāo),此時(shí)點(diǎn)擊清空?qǐng)D標(biāo)能清空文本框內(nèi)輸入文字。
正文
一、實(shí)現(xiàn)效果
二、實(shí)現(xiàn)代碼
綁定事件
復(fù)制代碼 代碼如下:
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);
}
觸摸事件
復(fù)制代碼 代碼如下:
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;
}
};
復(fù)制代碼 代碼如下:
//監(jiān)聽(tīng)輸入
/**
* 動(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) {
}
};
代碼說(shuō)明:
1.為輸入框綁定觸摸事件(模擬點(diǎn)擊事件捕捉)。通過(guò)監(jiān)聽(tīng)點(diǎn)擊區(qū)域判斷是否點(diǎn)擊清空?qǐng)D片,如果在該區(qū)域并且文本框不為空,則清空文本框。
2.為輸入框綁定文本改變事件監(jiān)聽(tīng),根據(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)搜索本地音樂(lè)的方法
相關(guān)文章
Android嵌套R(shí)ecyclerView左右滑動(dòng)替代自定義view
這篇文章主要介紹了Android嵌套R(shí)ecyclerView左右滑動(dòng)替代自定義view,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android實(shí)現(xiàn)多級(jí)樹(shù)形選擇列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)樹(shù)形選擇列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Android dataBinding與ListView及事件詳解
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
這篇文章主要為大家詳細(xì)介紹了Android獲取點(diǎn)擊屏幕的位置坐標(biāo),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
今天小編就為大家分享一篇關(guān)于Android Usb設(shè)備的監(jiān)聽(tīng)(Dev)外設(shè)端口的判定以及耳機(jī)的插拔,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Android開(kāi)發(fā)技巧之像QQ一樣輸入文字和表情圖像
QQ聊天輸入框,在輸入框中可以同時(shí)輸入文字和表情圖像。實(shí)際上,這種效果在Android SDK中只需要幾行代碼就可以實(shí)現(xiàn),本文將會(huì)介紹如何實(shí)現(xiàn)像QQ一樣輸入表情圖像2013-01-01

