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


二、實現(xiàn)代碼
綁定事件
private Drawable mIconSearchDefault; // 搜索文本框默認圖標
private Drawable mIconSearchClear; // 搜索文本框清除文本內(nèi)容圖標
@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è)置圖標顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
本文實現(xiàn)的效果:文本框輸入為空時顯示輸入的圖標;不為空時顯示清空的圖標,此時點擊清空圖標能清空文本框內(nèi)輸入文字。
正文
一、實現(xiàn)效果


二、實現(xiàn)代碼
綁定事件
復(fù)制代碼 代碼如下:
private Drawable mIconSearchDefault; // 搜索文本框默認圖標
private Drawable mIconSearchClear; // 搜索文本框清除文本內(nèi)容圖標
@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è)置圖標顯示。
3.維持清空操作后軟鍵盤狀態(tài)。
相關(guān)文章
Android點擊Button實現(xiàn)功能的幾種方法總結(jié)
當Button有多個或者Button的使用次數(shù)很多時,我們需要采用綁定監(jiān)聽器的做法,其實,綁定監(jiān)聽器也有幾種方法,不過,我在這里就不一一列舉了,畢竟那些方法在實際的應(yīng)用中也不常見2013-10-10Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the s
這篇文章主要介紹了 Android studio 出現(xiàn)錯誤Run with --stacktrace option to get the stack trace. Run with --info or --debu的相關(guān)資料,需要的朋友可以參考下2016-11-11Android實現(xiàn)Gesture手勢識別用法分析
這篇文章主要介紹了Android實現(xiàn)Gesture手勢識別用法,結(jié)合實例形式較為詳細的分析了Android基于Gesture實現(xiàn)手勢識別的原理與具體實現(xiàn)技巧,需要的朋友可以參考下2016-09-09Kotlin 創(chuàng)建接口或者抽象類的匿名對象實例
這篇文章主要介紹了Kotlin 創(chuàng)建接口或者抽象類的匿名對象實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03