最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)代碼
綁定事件
復(fù)制代碼 代碼如下:

private Drawable mIconSearchDefault; // 搜索文本框默認圖標(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)文章

最新評論

旬邑县| 洞口县| 来安县| 肃南| 铁岭县| 上虞市| 黄石市| 益阳市| 玉林市| 弥勒县| 景宁| 清新县| 邹平县| 凉山| 都兰县| 上林县| 大方县| 永定县| 金塔县| 南阳市| 乌兰县| 苍山县| 上饶市| 洛扎县| 麻江县| 隆德县| 英山县| 湘潭县| 铅山县| 曲阜市| 广东省| 屯留县| 积石山| 花莲县| 张家川| 伽师县| 博罗县| 汤原县| 同仁县| 永丰县| 焉耆|