当前位置: 移动技术网 > IT编程>移动开发>Android > Android中如何实现清空搜索框的文字

Android中如何实现清空搜索框的文字

2019年07月24日  | 移动技术网IT编程  | 我要评论

林政执法,幸福的我 歌词,龙漫电玩网

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

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

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

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网