0

我正在为 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;
        }

    }
}

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

4

1 回答 1

2

最后我找到了解决方案。我们必须传递 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;
        }
    }
于 2015-01-29T17:16:10.353 回答