首页 > 其他分享 >Android开发中的一个小功能 清空搜索框的文字

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

时间:2023-05-06 14:03:41浏览次数:31  
标签:文字 删除 int void ivDeleteText 空搜索 Android public View


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

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



[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开发中的一个小功能 清空搜索框的文字_EditText


标签:文字,删除,int,void,ivDeleteText,空搜索,Android,public,View
From: https://blog.51cto.com/u_16099425/6249598

相关文章

  • android 获取屏幕高度和宽度 的方法
    Android 获取屏幕高度和宽度的方法  我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就介绍讲一讲如何获取屏幕的物理尺寸   下面的代码即可获取屏幕的尺寸。    在一个Activity的onCreate方法中,写入如下代码:[java] vi......
  • Android 布局设计新思路
    [YouTube](AndroidSpeechRecognition-Part1-YouTube---Android语音识别-第1部分-YouTube)......
  • Android 横竖屏切换适配
    Android横竖屏幕切换(layout-land和layout-port)(betheme.net)  创建(layout-land和layout-port)文件夹 land是横向port是竖向 (332条消息)layout的横竖屏处理_layout横屏显示_ycb1689的博客-CSDN博客  ......
  • Android 屏幕适配
    一、概念1.1屏幕像素密度PPI、DPIPPI(PixelsPerInch)屏幕每英寸容纳多少个像素点,DPI(DotsPerInch)这个“点”是根据屏幕物理概念产生的一个软件概念,在不同行业有不同理解,印刷行业每英寸打印多少个墨点,鼠标移动一英寸光标移动多少像素点,在Android中被用来表示屏幕每英寸显示多......
  • typescript重写canvas --10.绘制文字2
    typescript重写canvas--10.绘制文字21.使用canvas绘制文字<!DOCTYPEHTML><html><head><metacharset="utf-8"/></head><body><canvasid="myCanvas"width="450"height="400">你的浏览器......
  • Android开发--WebView, WebChromeClient和WebViewClient
     WebView是AndroidAPP用来处理html页面的一个组件webview是android中的浏览器控件,在一些手机应用中常会用到b/s模式去开发应用,这时webview的强大功能就会显示出来。webview的几点设置   权限设置:.AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会......
  • Android开发--事件绑定及startActivity、startActivityForResult区别
      一、绑定事件的方法:1、方法一:UI组件事件属性调用main.xml:<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="forWard"an......
  • Android开发--架构详解
    Android体系架构详解:http://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/#ibm-pcon    src文件夹是项目的所有包及源文件(.java)(与一般的Java项目一样)分析HelloAndroid项目的主程序文件HelloAndroid.java,如代码清单3-8所示。代码清单3-8HelloAndroid.java......
  • deepspeech_android_devices_support
    AndroiddevicessupportWehavesupportforAndroidrelyingonTensorFlowLite,withJavaandJNIbindinds.Formoredetailsonhowtoexperimentwiththose,pleaserefertothesectionbelow.PleaserefertoTensorFlowdocumentationonhowtosetupthee......
  • Android 数据存储
    概述文件存储特点:openFileInput()和openFileOutput()读取设备上的文件。SharedPreferences特点:以XML格式将数据存储到设备。SQLite数据库特点:运算速度快,占用资源少,还支持基本SQL语法。ContentProvider特点:应用程序之间的数据交换,可以将自己的数据共享给其他应用......