android:imeOptions属性详解以及无效处理

一、键盘右下角的键设置

(1)actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED效果:

(2)actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE效果:
(3)actionGo去往,对应常量EditorInfo.IME_ACTION_GO 效果:
(4)actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH效果:
(5)actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND效果:
(6)actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT效果:
(7)actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE效果:

android:imeOptions=”flagNoExtractUi” //使软键盘不全屏显示,只占用一部分屏幕 同时,
这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions=”actionNone” //输入框右侧不带任何提示
android:imeOptions=”actionGo” //右下角按键内容为’开始’
android:imeOptions=”actionSearch” //右下角按键为放大镜图片,搜索
android:imeOptions=”actionSend” //右下角按键内容为’发送’
android:imeOptions=”actionNext” //右下角按键内容为’下一步’ 或者下一项
android:imeOptions=”actionDone” //右下角按键内容为’完成’

注意:如果设置了 键盘没有变化 那么需要单独加一些其他的属性 配合使用,一般添加android:inputType="text"就行了

xml中 属性设置:

1 将singleLine设置为true

2 将inputType设置为text

java代码设置
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH)

二、键盘的监听

1、设置监听 editText.setOnEditorActionListener(this);

2、重写onEditorAction

 @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        doWhichOperation(actionId);
        Log.e("BALLACK", "event: " + event);
        Log.e("BALLACK", "v.getImeActionId(): " + v.getImeActionId());
        Log.e("BALLACK", "v.getImeOptions(): " + v.getImeOptions());
        Log.e("BALLACK", "----------------------------------------------");
        return true;
    }

    private void doWhichOperation(int actionId) {
        switch (actionId) {
            case EditorInfo.IME_ACTION_DONE:
                Log.e("BALLACK", "IME_ACTION_DONE");
                break;
            case EditorInfo.IME_ACTION_GO:
                Log.e("BALLACK", "IME_ACTION_GO");
                break;
            case EditorInfo.IME_ACTION_NEXT:
                Log.e("BALLACK", "IME_ACTION_NEXT");
                break;
            case EditorInfo.IME_ACTION_NONE:
                Log.e("BALLACK", "IME_ACTION_NONE");
                break;
            case EditorInfo.IME_ACTION_PREVIOUS:
                Log.e("BALLACK", "IME_ACTION_PREVIOUS");
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                Log.e("BALLACK", "IME_ACTION_SEARCH");
                break;
            case EditorInfo.IME_ACTION_SEND:
                Log.e("BALLACK", "IME_ACTION_SEND");
                break;
            case EditorInfo.IME_ACTION_UNSPECIFIED:
                Log.e("BALLACK", "IME_ACTION_UNSPECIFIED");
                break;
            default:
                break;
        }

 

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果设置了`android:imeOptions="actionSearch"`属性,但点击软键盘上的回车键时没有触发搜索操作,可能有以下几种原因: 1. 没有设置OnEditorActionListener监听器 在设置了`android:imeOptions="actionSearch"`属性后,需要设置OnEditorActionListener监听器来监听搜索按钮的点击事件。例如: ``` EditText editText = findViewById(R.id.edit_text); editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 在这里处理搜索操作 return true; } return false; } }); ``` 2. 在布局文件中没有设置imeOptions属性 除了在EditText中设置`android:imeOptions="actionSearch"`属性,还需要在布局文件中设置相应的imeOptions属性。例如: ``` <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:inputType="text" /> ``` 3. 当前的软键盘不支持搜索按钮 某些软键盘可能不支持搜索按钮,因此需要使用支持搜索按钮的软键盘。可以尝试更换软键盘或者在代码中强制显示系统软键盘。例如: ``` InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); ``` 4. 在手机上运行的Android版本低于5.0 `android:imeOptions="actionSearch"`属性是在Android 5.0中引入的,如果在低版本的Android系统上运行,可能不支持该属性。可以尝试使用其他方式来实现搜索功能,例如在输入框旁边添加搜索按钮。 希望这些解决方法可以帮助你解决`android:imeOptions="actionSearch"`无效的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值