Android文本框搜索和清空效果實現(xiàn)代碼及簡要概述
更新時間:2013年02月16日 16:24:57 作者:
在工作過程中可能會遇到這樣一個效果:文本框輸入為空時顯示輸入的圖標(biāo);不為空時顯示清空的圖標(biāo),此時點擊清空圖標(biāo)能清空文本框內(nèi)輸入文字,感興趣的你可以了解下哦,或許對你學(xué)習(xí)android有所幫助
前言
本文實現(xiàn)的效果:文本框輸入為空時顯示輸入的圖標(biāo);不為空時顯示清空的圖標(biāo),此時點擊清空圖標(biāo)能清空文本框內(nèi)輸入文字。
正文
一、實現(xiàn)效果


二、實現(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à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)容改變動態(tài)改變列表內(nèi)容
*/
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
};
代碼說明:
1.為輸入框綁定觸摸事件(模擬點擊事件捕捉)。通過監(jiān)聽點擊區(qū)域判斷是否點擊清空圖片,如果在該區(qū)域并且文本框不為空,則清空文本框。
2.為輸入框綁定文本改變事件監(jiān)聽,根據(jù)內(nèi)容改變動態(tài)設(shè)置圖標(biāo)顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
本文實現(xiàn)的效果:文本框輸入為空時顯示輸入的圖標(biāo);不為空時顯示清空的圖標(biāo),此時點擊清空圖標(biāo)能清空文本框內(nèi)輸入文字。
正文
一、實現(xiàn)效果
二、實現(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à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)容改變動態(tài)改變列表內(nèi)容
*/
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
};
代碼說明:
1.為輸入框綁定觸摸事件(模擬點擊事件捕捉)。通過監(jiān)聽點擊區(qū)域判斷是否點擊清空圖片,如果在該區(qū)域并且文本框不為空,則清空文本框。
2.為輸入框綁定文本改變事件監(jiān)聽,根據(jù)內(nèi)容改變動態(tài)設(shè)置圖標(biāo)顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
相關(guān)文章
Android嵌套RecyclerView左右滑動替代自定義view
這篇文章主要介紹了Android嵌套RecyclerView左右滑動替代自定義view,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Android dataBinding與ListView及事件詳解
這篇文章主要介紹了Android dataBinding與ListView及事件詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android實現(xiàn)復(fù)制Assets文件到SD卡
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)復(fù)制Assets文件到SD卡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android Usb設(shè)備的監(jiān)聽(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
今天小編就為大家分享一篇關(guān)于Android Usb設(shè)備的監(jiān)聽(Dev)外設(shè)端口的判定以及耳機(jī)的插拔,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android開發(fā)技巧之像QQ一樣輸入文字和表情圖像
QQ聊天輸入框,在輸入框中可以同時輸入文字和表情圖像。實際上,這種效果在Android SDK中只需要幾行代碼就可以實現(xiàn),本文將會介紹如何實現(xiàn)像QQ一樣輸入表情圖像2013-01-01

