国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中如何實現清空搜索框的文字

2019-12-12 04:15:21
字體:
來源:轉載
供稿:網友

需求:項目中的有關搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗,使用戶刪除文本更加快捷

解決過程:開始的時候感覺這個東西不太好實現,主要就是布局的問題,可能是開始顧慮的太多了,再加上當時產品催的不太緊,而且這個功能也不是必須實現的。但是今天不一樣了,這個是老大讓加上的,說別的很多應用中都有這個功能,沒辦法那就加上唄,試著去使用了相對布局去實現,把一個刪除按鍵放在編輯框的右上方,當文字的時候就把刪除按鍵給顯示出來,當編輯框為空的時候就把刪除按鍵給隱藏掉。布局代碼

<?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="請輸入文字..."/>     <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> 

這代碼是直接從項目那截取過來的,里面用到了一些小技巧,開發的時候用到的布局寫法,其中以一種背景平鋪,這個在以前的文章里講述過。在主程序里主要是使用了EditText監聽輸入的功能,這個以前的文章也寫過,這次在使用又復習了一遍。代碼如下

[java] view plain copypublic 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);     }    }   }); 

現在就可以實現開始描述的要求了。這里面還用到了一張背景圖是.9.png的,能大能小哦

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 瓦房店市| 元朗区| 抚松县| 库车县| 宣化县| 长沙县| 平乐县| 江阴市| 台山市| 武乡县| 柏乡县| 濮阳市| 隆子县| 卢氏县| 京山县| 吉林市| 湘潭县| 临汾市| 宕昌县| 东乡| 陵川县| 农安县| 徐汇区| 苏尼特左旗| 平谷区| 张掖市| 泽普县| 文化| 四平市| 台州市| 三江| 邛崃市| 绥芬河市| 普宁市| 呼图壁县| 灯塔市| 尤溪县| 玉龙| 运城市| 永顺县| 哈巴河县|