Android-android 如何禁用软键盘中的确定键

Android-android 如何禁用软键盘中的确定键

浮生未歇 发布于 2016-10-10 字数 195 浏览 1124 回复 1

当进入一个Activity时,有个EditText,获得焦点然后弹出软键盘,我想当EditText中没有内容时,弹出的软键盘中的确定键消失或禁用(置灰),当输入内容时确定键自动出现或启动(不置灰)?请问如何实现?

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2017-05-30 1 楼

可以实现类似的操作,但是不能帮你把确认键弄消失,只是让他不管用而已
在 EditText 布局中添加一个 属性 值就可以的说~

android:imeOptions="actionNone|flagNoEnterAction"

google 官方的解释:

These options are controlled with the android:imeOptions attribute on TextView. The value you supply here can be any combination of:

One of the pre-defined action constants (actionGo, actionSearch, actionSend, actionNext, actionDone). If none of these are specified, the system will infer either actionNext or actionDone depending on whether there is a focusable field after this one; you can explicitly force no action with actionNone.
The flagNoEnterAction option tells the IME that the action should not be available on the enter key, even if the text itself is not multi-line. This avoids having unrecoverable actions like (send) that can be accidentally touched by the user while typing.
The flagNoAccessoryAction removes the action button from the text area, leaving more room for text.
The flagNoExtractUi completely removes the text area, allowing the application to be seen behind it

理论上,单纯的只设置 actionNone 其实就可以实现了,加上 flagNoEnterAction 可以防止那些不可挽回的操作,比如发送。

至于说待用户输入了内容后,通过监听当前控件的内容,
来修改会你需要的状态,只需要 java 代码:

 editText.setImeOptions(imeOptions);

恢复 normal 就 ok 了,祝好
斑驳敬上