Android EditText之软键盘搜索

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yechaoa/article/details/82837511

一个很常见的小功能,输入信息以后,点击软键盘的回车键可以执行相关操作,比如搜索,输入关键词之后,点击软键盘的搜索按钮(回车键)就可以执行搜索操作。

1.EditText

<EditText
   android:id="@+id/et_search_content"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginLeft="@dimen/dp_10"
   android:layout_marginRight="@dimen/dp_10"
   android:background="@null"
   android:ellipsize="end"
   android:hint="@string/search"
   android:imeOptions="actionSearch"
   android:inputType="text"
   android:maxLines="1"
   android:textSize="@dimen/sp_14"/>

android:imeOptions="actionSearch" 主要是这个属性,就是把软键盘上的回车键改成搜索按钮
相关的属性值还有
在这里插入图片描述

2.监听按钮事件

        etSearchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    YUtils.closeSoftKeyboard();
                    //do something
                    //doSearch();
                    ToastUtil.showToast("点击了软键盘的搜索按钮");
                    return true;
                }
                return false;
            }
        });

EditText继承TextView,所以这个事件是TextView中的
在这里插入图片描述


更多关于EditText的设置:EditText默认不获取焦点,隐藏软键盘,焦点监听,输入监听,样式设置


猜你喜欢

转载自blog.csdn.net/yechaoa/article/details/82837511