Android 自定义搜索栏

Android 自定义搜索栏(没有自动提示)


点击键盘回车键开始搜索

自定义搜索框样式

样式可以自己自定义,看你喜欢咯

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/focusLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/y60"
    android:background="@drawable/rounded_searchview_default_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/x20"
        android:src="@mipmap/ic_search_app_left" />

    <EditText
        android:id="@+id/searchEdit"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/x10"
        android:background="@color/transparent"
        android:hint="@string/searchview_query_hint"
        android:imeOptions="actionSearch"
        android:inputType="text"
        android:maxLines="1"
        android:textColor="@color/search_view_background"
        android:textColorHint="@color/search_view_background"
        android:textSize="13sp" />

</LinearLayout>

引用自定义搜索框样式

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:background="@color/white"
        android:paddingLeft="@dimen/x30"
        android:paddingTop="@dimen/y15"
        android:paddingRight="@dimen/x30"
        android:paddingBottom="@dimen/y15">

        <include layout="@layout/base_searchview" />

    </FrameLayout>

使用

实现接口
其中searchEdit.getText().toString()获取的是输入的关键字

implements TextView.OnEditorActionListener

 @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN)) {
           String string = searchEdit.getText().toString();
            try {
                KeyBoardUtils.closeKeybord(searchEdit, this);//关闭键盘
            } catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
        return false;
    }

绑定控件

@BindView(R.id.searchEdit)
    EditText searchEdit;

绑定事件

searchEdit.setOnEditorActionListener(this);

关闭键盘

public static void closeKeybord(EditText mEditText, Context mContext)
    {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值