Android中如何實(shí)現(xiàn)清空搜索框的文字
需求:項(xiàng)目中的有關(guān)搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗(yàn),使用戶刪除文本更加快捷
解決過程:開始的時(shí)候感覺這個(gè)東西不太好實(shí)現(xiàn),主要就是布局的問題,可能是開始顧慮的太多了,再加上當(dāng)時(shí)產(chǎn)品催的不太緊,而且這個(gè)功能也不是必須實(shí)現(xiàn)的。但是今天不一樣了,這個(gè)是老大讓加上的,說別的很多應(yīng)用中都有這個(gè)功能,沒辦法那就加上唄,試著去使用了相對(duì)布局去實(shí)現(xiàn),把一個(gè)刪除按鍵放在編輯框的右上方,當(dāng)文字的時(shí)候就把刪除按鍵給顯示出來,當(dāng)編輯框?yàn)榭盏臅r(shí)候就把刪除按鍵給隱藏掉。布局代碼
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:paddingBottom="50dp" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/top" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="@drawable/top_background" android:layout_height="wrap_content"> <Button android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:textSize="12sp" android:textStyle="bold" android:background="@drawable/search_btn_background" android:text="搜索"/> <RelativeLayout android:id="@+id/rlSearchFrameDelete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center_vertical" android:layout_toLeftOf="@id/btnSearch"> <EditText android:id="@+id/etSearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" android:background="@drawable/search_frame" android:layout_marginRight="10dp" android:paddingLeft="32dp" android:textSize="12sp" android:hint="請(qǐng)輸入文字..."/> <ImageView android:id="@+id/ivDeleteText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:src="@drawable/delete" android:layout_centerInParent="true" android:paddingRight="20dp" android:visibility="gone"/> </RelativeLayout> </RelativeLayout> </RelativeLayout>
這代碼是直接從項(xiàng)目那截取過來的,里面用到了一些小技巧,開發(fā)的時(shí)候用到的布局寫法,其中以一種背景平鋪,這個(gè)在以前的文章里講述過。在主程序里主要是使用了EditText監(jiān)聽輸入的功能,這個(gè)以前的文章也寫過,這次在使用又復(fù)習(xí)了一遍。代碼如下
[java] view plain copy public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText); etSearch = (EditText) findViewById(R.id.etSearch); ivDeleteText.setOnClickListener(new OnClickListener() { public void onClick(View v) { etSearch.setText(""); } }); etSearch.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void afterTextChanged(Editable s) { if (s.length() == 0) { ivDeleteText.setVisibility(View.GONE); } else { ivDeleteText.setVisibility(View.VISIBLE); } } });
現(xiàn)在就可以實(shí)現(xiàn)開始描述的要求了。這里面還用到了一張背景圖是.9.png的,能大能小哦
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android SearchView搜索框組件的使用方法
- Android搜索框通用版
- Android搜索框組件SearchView的基本使用方法
- Android EditText搜索框?qū)崿F(xiàn)圖標(biāo)居中
- android搜索框上下滑動(dòng)變色效果
- Android搜索框SearchView屬性和用法詳解
- Android搜索框(SearchView)的功能和用法詳解
- Android頂部(toolbar)搜索框?qū)崿F(xiàn)的實(shí)例詳解
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android 改變圖標(biāo)原有顏色和搜索框的實(shí)例代碼
- Android實(shí)現(xiàn)實(shí)時(shí)搜索框功能
- Android編程自定義搜索框?qū)崿F(xiàn)方法【附demo源碼下載】
相關(guān)文章
Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation)基礎(chǔ)學(xué)習(xí)
大家都知道逐幀動(dòng)畫是一種常見的動(dòng)畫形式,其原理是在“連續(xù)的關(guān)鍵幀”中分解動(dòng)畫動(dòng)作,也就是在時(shí)間軸的每幀上逐幀繪制不同的內(nèi)容,使其連續(xù)播放而成動(dòng)畫。下面我們就來學(xué)習(xí)下Android中逐幀動(dòng)畫的基礎(chǔ)知識(shí),有需要的可以參考借鑒。2016-09-09Android?App實(shí)現(xiàn)閃屏頁廣告圖的全屏顯示實(shí)例
這篇文章主要為大家介紹了Android?App實(shí)現(xiàn)閃屏頁廣告圖的全屏顯示實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)
這篇文章主要介紹了Android獲取當(dāng)前位置的經(jīng)緯度數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-02-02Android Studio如何為Activity添加自定義注解信息
好久沒用寫文章了,今天給大家分享Android Studio如何為Activity添加自定義注解信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06Android簡單的短信驗(yàn)證功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android簡單的短信驗(yàn)證功能的實(shí)現(xiàn)代碼,本文是小編使用sdk過程的一些心得,需要的朋友可以參考下2018-07-07Android Viewpager實(shí)現(xiàn)無限循環(huán)輪播圖
這篇文章主要為大家詳細(xì)介紹了Android Viewpager實(shí)現(xiàn)無限循環(huán)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽
這篇文章主要為大家詳細(xì)介紹了Android使用觀察者模式Observer實(shí)現(xiàn)網(wǎng)絡(luò)狀態(tài)監(jiān)聽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android解析json數(shù)組對(duì)象的方法及Apply和數(shù)組的三個(gè)技巧
這篇文章主要介紹了Android解析json數(shù)組對(duì)象的方法及Apply和數(shù)組的三個(gè)技巧的相關(guān)資料,需要的朋友可以參考下2015-12-12