gpt4 book ai didi

android - 如何在 android 中为子类型加载不同的键盘布局?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:22:22 24 4
gpt4 key购买 nike

我正在使用 android 示例 keyboard .它包括 En (US) 和 En (GB) subtypes。在选择任一 subtype 时,它只会更改空格键上的标志。

假设我想根据所选的子类型更改布局,但我无法这样做。

到目前为止,我已经为英语 (GB) 创建了另一个 xml 文件,我将其命名为 qwerty_gb.xml(出于测试目的,我交换了 ReturnDel 键)

然后声明为私有(private)

LatinKeyboard mQwertyKeyboardGB;

并在 Softkeyboard.java 的 onInitializeInterface 覆盖方法中将其与默认键盘一起初始化

像这样:

mQwertyKeyboardGB = new LatinKeyboard(this, R.xml.qwerty_gb);

我不知道我在这里错过了什么。

最佳答案

使用以下修改编辑 SoftKeyboard.java 文件,以便为每个子类型设置特定的布局。

1- 首先引用您的布局。

mQwertyKeyboard = new LatinKeyboard(this, R.xml.qwerty);
mPersianKeyboard = new LatinKeyboard(this, R.xml.persian);

2- 在 OnCreateInputView 中添加以下内容以应用正确的布局。

InputMethodSubtype subtype = mInputMethodManager.getCurrentInputMethodSubtype();
switch(subtype.getLocale()) {
case "fa_IR":
setLatinKeyboard(mPersianKeyboard);
break;
case "en_US":
setLatinKeyboard(mQwertyKeyboard);
break;
}

如果语言环境是 fa_IR,上面的代码应用 mPersianKeyboard。语言环境 fa_IRmethod.xml 中设置。

<subtype
android:label="@string/label_subtype_generic"
android:icon="@drawable/icon_en_us"
android:imeSubtypeLocale="en_US"
android:imeSubtypeMode="keyboard" />
<subtype
android:label="@string/label_subtype_generic"
android:icon="@drawable/icon_en_gb"
android:imeSubtypeLocale="fa_IR"
android:imeSubtypeMode="keyboard" />

3- 最后修改 `onCurrentInputMethodSubtypeChanged 方法如下:

@Override
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) {
mInputView.setSubtypeOnSpaceKey(subtype);
switch(subtype.getLocale()) {
case "fa_IR":
setLatinKeyboard(mSymbolsKeyboard);
break;
case "en_US":
setLatinKeyboard(mQwertyKeyboard);
break;
};
}

注意:getLocale() 方法在 API 级别 24 中已弃用。请改用 getLanguageTag()。最好是检查版本,对正确的版本使用正确的方法。

关于android - 如何在 android 中为子类型加载不同的键盘布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32848825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com