android 软键盘回车键修改方法与监听

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:41   1635   0

最近公司UI 要求在键盘输入时为了布局的美观,最好可以更改回车键为搜索和完成,并对相关的事件做处理。 经过查找资料,在EditText中有个imeOptions方法可以修改回车键显示内容。 imeOptions 参数主要有:actionDone ,actionGo ,actionNext, actionSearch ,actionSend等,具体可以查看相关的文档https://developer.android.com/training/keyboard-input/style.htmlhttps://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions) 。里面有详细的介绍使用方法

下面以actionDone 为例子介绍相关的使用方法。 布局文件如下:

<EditText
                android:id="@+id/storage_size_tv"
                android:layout_width="90dp"
                android:gravity="right"
                android:layout_height="wrap_content"
                android:imeOptions="actionDone"
                android:inputType="number"
                android:maxLines="1"
                android:background="@null" />

code:主要是对EditText设置OnEditorActionListener,监听回车键的触发做相应的操作

storage_size_tv.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == KeyEvent.KEYCODE_ENTER){//这里可以做具体的操作
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                }

                return false;
            }
        });

具体的效果如下图所示:

输入图片说明

ps:官方文档是个好东西,有空时候还是要多看看

转载于:https://my.oschina.net/kipeng/blog/831964

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP