问题描述
我正在为Android设计自定义键盘.我想为我的应用程序中的某些字段输入用于ENTER键的自定义标签.我使用了示例SoftKeyboard项目来开发键盘.到目前为止我尝试过的是:1-在我的一项活动中,我有一个具有以下属性的EditText:
I'm designing a custom keyboard for Android. I want to have my custom label for ENTER key for some fields in my application. I used sample SoftKeyboard project for developing my keyboard.What I tried so far:1- In one of my activities I have an EditText with the following attributes:
<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键的默认值:
If I use Native Android keyboard it shows "Sign In" on my enter key but if I use my custom keyboard it shows the default value of enter key in the following statement:
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;
}
}
}
如果有人可以帮助我解决此问题,我将不胜感激.
I would appreciate if someone can help me to solve this issue.
推荐答案
最后,我找到了解决方案.而不是传递int选项,我们必须传递EditorInfo属性.我们像波纹管一样传递它
Finally I found the solution. Instead of passing int options, we have to pass EditorInfo attribute. We pass it like bellow
@Override
public void onStartInput(EditorInfo attribute, boolean restarting)
{
super.onStartInput(attribute, restarting);
...
yourSoftKeyboard.setImeOptions(getResources(), attribute);
}
然后我们像下面这样实现setImeOptions:
Then we implement setImeOptions like bellow:
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;
}
}
这篇关于无法更改Android中自定义键盘的Enter键标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!