android简单自定义搜索框

搜索功能类:

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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值