0
点赞
收藏
分享

微信扫一扫

Android开发中的一个小功能 清空搜索框的文字


需求:项目中的有关搜索的地方,加上清空文字的功能,目的是为了增加用户体验,使用户删除文本更加快捷

解决过程:开始的时候感觉这个东西不太好实现,主要就是布局的问题,可能是开始顾虑的太多了,再加上当时产品催的不太紧,而且这个功能也不是必须实现的。但是今天不一样了,这个是老大让加上的,说别的很多应用中都有这个功能,没办法那就加上呗,试着去使用了相对布局去实现,把一个删除按键放在编辑框的右上方,当文字的时候就把删除按键给显示出来,当编辑框为空的时候就把删除按键给隐藏掉。布局代码



[html]  view plain  copy



这代码是直接从项目那截取过来的,里面用到了一些小技巧,开发的时候用到的布局写法,其中以一种背景平铺,这个在以前的文章里讲述过。在主程序里主要是使用了EditText监听输入的功能,这个以前的文章也写过,这次在使用又复习了一遍。代码如下

1. <?xml version="1.0" encoding="utf-8"?>  
2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3. android:orientation="vertical"  
4. android:paddingBottom="50dp"  
5. android:layout_width="fill_parent"  
6. android:layout_height="fill_parent">  
7.       
8. <RelativeLayout android:id="@+id/top"  
9. android:layout_width="fill_parent"  
10. android:layout_alignParentTop="true"  
11. android:paddingLeft="10dp"  
12. android:paddingRight="10dp"  
13. android:background="@drawable/top_background"  
14. android:layout_height="wrap_content">  
15.           
16. <Button android:id="@+id/btnSearch"  
17. android:layout_width="wrap_content"  
18. android:layout_height="wrap_content"  
19. android:gravity="center"  
20. android:layout_centerVertical="true"  
21. android:layout_alignParentRight="true"  
22. android:textSize="12sp"  
23. android:textStyle="bold"  
24. android:background="@drawable/search_btn_background"  
25. android:text="搜索"/>  
26.           
27. <RelativeLayout android:id="@+id/rlSearchFrameDelete"  
28. android:layout_width="fill_parent"  
29. android:layout_height="wrap_content"  
30. android:layout_centerVertical="true"  
31. android:gravity="center_vertical"  
32. android:layout_toLeftOf="@id/btnSearch">  
33.               
34. <EditText android:id="@+id/etSearch"  
35. android:layout_width="fill_parent"  
36. android:layout_height="wrap_content"  
37. android:singleLine="true"  
38. android:background="@drawable/search_frame"  
39. android:layout_marginRight="10dp"  
40. android:paddingLeft="32dp"  
41. android:textSize="12sp"  
42. android:hint="请输入文字..."/>  
43.                   
44. <ImageView android:id="@+id/ivDeleteText"  
45. android:layout_width="wrap_content"  
46. android:layout_height="wrap_content"  
47. android:layout_alignParentRight="true"  
48. android:src="@drawable/delete"  
49. android:layout_centerInParent="true"  
50. android:paddingRight="20dp"  
51. android:visibility="gone"/>  
52.               
53. </RelativeLayout>  
54.           
55.           
56. </RelativeLayout>  
57.       
58. </RelativeLayout>




[java]  view plain  copy



1. public void onCreate(Bundle savedInstanceState) {  
2. super.onCreate(savedInstanceState);  
3.         setContentView(R.layout.activity_main);  
4.         ivDeleteText = (ImageView) findViewById(R.id.ivDeleteText);  
5.         etSearch = (EditText) findViewById(R.id.etSearch);  
6.           
7. new OnClickListener() {  
8.               
9. public void onClick(View v) {  
10. "");  
11.             }  
12.         });  
13.           
14. new TextWatcher() {  
15.               
16. public void onTextChanged(CharSequence s, int start, int before, int count) {  
17. // TODO Auto-generated method stub  
18.                   
19.             }  
20.               
21. public void beforeTextChanged(CharSequence s, int start, int count,  
22. int after) {  
23. // TODO Auto-generated method stub  
24.                   
25.             }  
26.               
27. public void afterTextChanged(Editable s) {  
28. if (s.length() == 0) {  
29.                     ivDeleteText.setVisibility(View.GONE);  
30. else {  
31.                     ivDeleteText.setVisibility(View.VISIBLE);  
32.                 }  
33.             }  
34.         });



现在就可以实现开始描述的要求了。这里面还用到了一张背景图是.9.png的,能大能小哦 

Demo代码:

Android开发中的一个小功能 清空搜索框的文字_xml


举报

相关推荐

思科APIC的小功能之一

各种小功能的实现

0 条评论