android简单自定义搜索框

来源:互联网 发布:联通网络加速器 编辑:程序博客网 时间:2024/05/20 15:42

搜索功能类:

public class SearchBuilder {    private LinearLayout search_bar;    private TextView hint;    public SearchBuilder(Activity context){        search_bar = (LinearLayout) context.findViewById(R.id.search_bar_id);        hint = (TextView) context.findViewById(R.id.search_bar_text);    }    public SearchBuilder(View context){        search_bar = (LinearLayout) context.findViewById(R.id.search_bar_id);        hint = (TextView) context.findViewById(R.id.search_bar_text);    }    //搜索按钮监听    public SearchBuilder QueryOnListener(View.OnClickListener listener){        if (search_bar.getVisibility() == View.VISIBLE){            search_bar.setOnClickListener(listener);        }        return this;    }    //设置提示内容    public SearchBuilder setHintText(String hintText){        if (hint.getVisibility() == View.VISIBLE){            hint.setText(hintText);        }        return this;    }}

layout xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="40dp"    android:paddingLeft="8dp"    android:paddingRight="8dp"    android:layout_weight="1">        <LinearLayout            android:id="@+id/search_bar_id"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="horizontal"            android:paddingLeft="8dp"            android:paddingRight="8dp"            android:gravity="center"            android:background="@drawable/text_rectangle_white">            <ImageView                android:layout_width="24dp"                android:layout_height="24dp"                android:layout_gravity="center"                android:background="@drawable/search"/>            <TextView                android:id="@+id/search_bar_text"                android:text="seach test"                android:paddingLeft="16dp"                android:layout_weight="1"                android:gravity="center|left"                android:layout_width="wrap_content"                android:layout_height="match_parent" />        </LinearLayout></LinearLayout>

text_rectangle_white:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle"    android:useLevel="false">    <!-- 实心 -->    <solid android:color="@color/white" />    <!-- 圆角 -->    <corners android:radius="24dp" />    <!-- 边距 -->    <padding        android:bottom="1dp"        android:left="1dp"        android:right="1dp"        android:top="1dp" />    <!--边框线-->    <stroke        android:width="0.5dp"        android:color="@color/gray" />    <!-- 大小 -->    <size android:width="96dp"        android:height="16dp" /></shape>




0 0