【问题标题】:Cannot change Enter Key label for Custom keyboard in Android无法更改 Android 中自定义键盘的 Enter 键标签
【发布时间】:2014-11-13 05:29:21
【问题描述】:

我正在为 Android 设计一个自定义键盘。我想为我的应用程序中的某些字段设置 ENTER 键的自定义标签。我使用示例 SoftKeyboard 项目来开发我的键盘。 到目前为止我尝试了什么: 1- 在我的一项活动中,我有一个具有以下属性的 EditText:

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:imeActionId="@+id/action_sign_in"
    android:imeActionLabel="@string/sign_in"

    android:inputType="textPassword" />

如果我使用原生 Android 键盘,它会在我的 enter 键上显示“登录”,但如果我使用我的自定义键盘,它会在以下语句中显示 enter 键的默认值:

LatinKeyboard.java

void setImeOptions(Resources res, int options)
    {
        if (mEnterKey == null)
        {
            return;
        }

        switch (options & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION))
        {
            case EditorInfo.IME_ACTION_GO:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_send_key);
                break;
            case EditorInfo.IME_ACTION_NEXT:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_next_key);
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                mEnterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
                mEnterKey.label = null;
                break;
            case EditorInfo.IME_ACTION_SEND:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.label_send_key);
                break;
            case R.id.action_sign_in:
                mEnterKey.iconPreview = null;
                mEnterKey.icon = null;
                mEnterKey.label = res.getText(R.string.sign_in);
                break;
            default:
                mEnterKey.label = res.getText(R.string.label_send_key);
                mEnterKey.icon = null;
                break;
        }

    }
}

如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

    标签: android android-softkeyboard custom-keyboard imeoptions


    【解决方案1】:

    我终于找到了解决方案。我们必须传递 EditorInfo 属性,而不是传递 int 选项。我们像下面这样传递它

    @Override
        public void onStartInput(EditorInfo attribute, boolean restarting)
        {
            super.onStartInput(attribute, restarting);
             ...
            yourSoftKeyboard.setImeOptions(getResources(), attribute);
    }
    

    然后我们像下面这样实现 setImeOptions:

    void setImeOptions(Resources res, EditorInfo ei)
        {
            if (enterKey == null)
            {
                return;
            }
    
            switch (ei.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION))
            {
                case EditorInfo.IME_ACTION_SEND:
                    enterKey.iconPreview = null;
                    enterKey.icon = null;
                    enterKey.label ="Send";
                    break;
                case EditorInfo.IME_ACTION_GO:
                    enterKey.iconPreview = null;
                    enterKey.icon = null;
                    enterKey.label ="Go";
                    break;
                case EditorInfo.IME_ACTION_NEXT:
                    enterKey.iconPreview = null;
                    enterKey.icon = null;
                    enterKey.label = "Next";
                    break;
                case EditorInfo.IME_ACTION_SEARCH:
                    enterKey.icon = res.getDrawable(R.drawable.sym_keyboard_search);
                    enterKey.label = null;
                    break;
                default:
                    enterKey.iconPreview = null;
                    enterKey.label = "Enter";
                    enterKey.icon = null;
                    break;
            }
    
            if (ei.actionLabel != null)
            {
                enterKey.iconPreview = null;
                enterKey.icon = null;
                enterKey.label = ei.actionLabel;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2019-08-20
      • 2012-06-27
      • 2019-05-26
      • 2013-09-15
      • 2016-12-03
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多