无法在Android中更改自定义键盘的Enter Key标签

时间:2014-09-18 18:37:37

标签: android android-softkeyboard custom-keyboard imeoptions

我正在为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键盘,它会显示&#34;登录&#34;在我的回车键上,但如果我使用我的自定义键盘,它会在以下语句中显示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;
        }

    }
}

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

1 个答案:

答案 0 :(得分:2)

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

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

然后我们实现了像bellow:

这样的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;
        }
    }