日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

[Android]文本框实现搜索和清空效果

發(fā)布時(shí)間:2025/3/20 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Android]文本框实现搜索和清空效果 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

  本文實(shí)現(xiàn)的效果:文本框輸入為空時(shí)顯示輸入的圖標(biāo);不為空時(shí)顯示清空的圖標(biāo),此時(shí)點(diǎn)擊清空?qǐng)D標(biāo)能清空文本框內(nèi)輸入文字。
?

?

聲明

  歡迎轉(zhuǎn)載,但請(qǐng)保留文章原始出處:)
?

  農(nóng)民伯伯:http://over140.blog.51cto.com/

?

正文

  一、實(shí)現(xiàn)效果
?

    

       

?

  二、實(shí)現(xiàn)代碼
?

    監(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)?{
????????????
????????}
????};

?    觸摸事件

????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;
????????}
????};

    綁定事件

????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);
????}

    代碼說(shuō)明:

      1. 為輸入框綁定觸摸事件(模擬點(diǎn)擊事件捕捉)。通過(guò)監(jiān)聽點(diǎn)擊區(qū)域判斷是否點(diǎn)擊清空?qǐng)D片,如果在該區(qū)域并且文本框不為空,則清空文本框。
?

      2. 為輸入框綁定文本改變事件監(jiān)聽,根據(jù)內(nèi)容改變動(dòng)態(tài)設(shè)置圖標(biāo)顯示。

      3. 維持清空操作后軟鍵盤狀態(tài)。

?

  三、參考

    1.? how to block virtual keyboard while clicking on edittext in android?

?

  四、小圖標(biāo)下載

      

    (右鍵另存為即可。)

?

結(jié)束
?

  活用好每一個(gè)控件的屬性、方法和事件能實(shí)現(xiàn)很多有意思的效果。歡迎大家交流。
?

轉(zhuǎn)載于:https://blog.51cto.com/over140/581678

總結(jié)

以上是生活随笔為你收集整理的[Android]文本框实现搜索和清空效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。