超简单的搜索框

搜索功能类:

[java]  view plain  copy
  1. public class SearchBuilder {  
  2.     private LinearLayout search_bar;  
  3.     private TextView hint;  
  4.     public SearchBuilder(Activity context){  
  5.         search_bar = (LinearLayout) context.findViewById(R.id.search_bar_id);  
  6.         hint = (TextView) context.findViewById(R.id.search_bar_text);  
  7.     }  
  8.   
  9.     public SearchBuilder(View context){  
  10.         search_bar = (LinearLayout) context.findViewById(R.id.search_bar_id);  
  11.         hint = (TextView) context.findViewById(R.id.search_bar_text);  
  12.     }  
  13.     //搜索按钮监听  
  14.     public SearchBuilder QueryOnListener(View.OnClickListener listener){  
  15.         if (search_bar.getVisibility() == View.VISIBLE){  
  16.             search_bar.setOnClickListener(listener);  
  17.         }  
  18.         return this;  
  19.     }  
  20.   
  21.     //设置提示内容  
  22.     public SearchBuilder setHintText(String hintText){  
  23.         if (hint.getVisibility() == View.VISIBLE){  
  24.             hint.setText(hintText);  
  25.         }  
  26.         return this;  
  27.     }  
  28. }  

layout xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="40dp"  
  6.     android:paddingLeft="8dp"  
  7.     android:paddingRight="8dp"  
  8.     android:layout_weight="1">  
  9.         <LinearLayout  
  10.             android:id="@+id/search_bar_id"  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="match_parent"  
  13.             android:orientation="horizontal"  
  14.             android:paddingLeft="8dp"  
  15.             android:paddingRight="8dp"  
  16.             android:gravity="center"  
  17.             android:background="@drawable/text_rectangle_white">  
  18.             <ImageView  
  19.                 android:layout_width="24dp"  
  20.                 android:layout_height="24dp"  
  21.                 android:layout_gravity="center"  
  22.                 android:background="@drawable/search"/>  
  23.             <TextView  
  24.                 android:id="@+id/search_bar_text"  
  25.                 android:text="seach test"  
  26.                 android:paddingLeft="16dp"  
  27.                 android:layout_weight="1"  
  28.                 android:gravity="center|left"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="match_parent" />  
  31.         </LinearLayout>  
  32. </LinearLayout>  

 
 
text_rectangle_white:
 
 
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle"  
  4.     android:useLevel="false">  
  5.     <!-- 实心 -->  
  6.     <solid android:color="@color/white" />  
  7.     <!-- 圆角 -->  
  8.     <corners android:radius="24dp" />  
  9.     <!-- 边距 -->  
  10.     <padding  
  11.         android:bottom="1dp"  
  12.         android:left="1dp"  
  13.         android:right="1dp"  
  14.         android:top="1dp" />  
  15.     <!--边框线-->  
  16.     <stroke  
  17.         android:width="0.5dp"  
  18.         android:color="@color/gray" />  
  19.     <!-- 大小 -->  
  20.     <size android:width="96dp"  
  21.         android:height="16dp" />  
  22. </shape>  

猜你喜欢

转载自blog.csdn.net/qq_40702815/article/details/80143571