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);
}