亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中如何實(shí)現(xiàn)清空搜索框的文字

 更新時(shí)間:2016年12月21日 15:08:01   作者:木頭人__walker  
本文主要介紹Android中實(shí)現(xiàn)清空搜索框的文字的方法。項(xiàng)目中的有關(guān)搜索的地方,加上清空文字的功能,目的是為了增加用戶體驗(yà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í)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論