美文网首页
EditText软键盘右下角按钮设置

EditText软键盘右下角按钮设置

作者: 贝贝ovo | 来源:发表于2016-11-12 16:47 被阅读2438次

首先,养个好习惯,只要使用EditText 就在XML文件中声明要输入的文本类型。官方文档也说了 always

always.png

ok,回归主题,软键盘右下角按钮,默认情况下是一个Enter符号,作用换行。


defult androidimeOptions.png

EditText提供了androidimeOptions属性,控制该按钮的显示文字。(仅以搜狗为例,其他的界面显示可能不同,大致意思是一样的)

android:imeOptions 文字
actionGo 开始
actionNext 下一步
actionSearch 搜索
actionSend 发送
actionDone Enter符号

注意:要使android:imeOptions起作用,必须能加上android:inputType属性,这也是一开始强调的 或者加上android:singleLine="true"也可以,但用android:maxLines="1"不可以

然后给右下角按钮设置点击监听事件,隐藏软键盘

mActionSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    Toast.makeText(MainActivity.this, "搜索", Toast.LENGTH_SHORT).show();
                    handled = true;

                     /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
                    }
                }
                return handled;
            }
        });

2017.1.13补充
我们会发现我们在EditText中输入文字的时候,光标会随着你文字的增加一直往后移动,但是在有的android版本中,它并不会换行,其中有种有种可能是你把Enter键给设置成了”完成”, android:imeOptions=”actionDone” ,如果这是你需要换行的话,只要改变输入的类型即可: android:inputType=”textMultiLine”(表示多行输入)

Specifying the Input Method Type 官网链接

相关文章

网友评论

      本文标题:EditText软键盘右下角按钮设置

      本文链接:https://www.haomeiwen.com/subject/jxdqpttx.html