LatinIME输入法增加不同国家字符键盘

1.打开method.xml

要创建什么语言的键盘就查找对应的缩写词,这里以巴西为例修改KeyboardLayoutSet=brazil

2.attrs.xml文件supportedScript枚举项添加对应的支持

3.创建对应的键盘布局文件keyboard_layout_set_brazil.xml

keyboard_layout_set_xxxx.xml   后缀要接定义的brazil字段,需要制作自己需要的键盘布局就可以了

<KeyboardLayoutSet
    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin">
    <Feature
        latin:supportedScript="brazil" />
    <Element
        latin:elementName="alphabet"
        latin:elementKeyboard="@xml/kbd_brazil"
        latin:enableProximityCharsCorrection="true" />
    <Element
        latin:elementName="symbols"
        latin:elementKeyboard="@xml/kbd_symbols" />
    <Element
        latin:elementName="symbolsShifted"
        latin:elementKeyboard="@xml/kbd_symbols_shift" />
    <Element
        latin:elementName="phone"
        latin:elementKeyboard="@xml/kbd_phone" />
    <Element
        latin:elementName="phoneSymbols"
        latin:elementKeyboard="@xml/kbd_phone_symbols" />
    <Element
        latin:elementName="number"
        latin:elementKeyboard="@xml/kbd_number" />
</KeyboardLayoutSet>

4.在ScriptUtils.java文件也需要增加相应的字段,不然会报错

最后附上diff

From 5fcbfdf3a6f1780afb46d84685980f0f83fd4b38 Mon Sep 17 00:00:00 2001
From: zhouweiyang <weiyang.zhou@stellamore.cn>
Date: Sat, 15 Jun 2024 11:12:56 +0800
Subject: [PATCH] =?UTF-8?q?feat=20[LatinIME]=20LatinIME=E8=BE=93=E5=85=A5?=
 =?UTF-8?q?=E6=B3=95=E5=A2=9E=E5=8A=A0=E5=B7=B4=E8=A5=BF=E9=94=AE=E7=9B=98?=
 =?UTF-8?q?=E9=80=82=E9=85=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../LatinIME/java/res/values-pt-rPT/bools.xml | 24 ++++++++
 .../LatinIME/java/res/values/attrs.xml        |  1 +
 .../LatinIME/java/res/xml/kbd_brazil.xml      | 26 ++++++++
 .../res/xml/keyboard_layout_set_brazil.xml    | 44 ++++++++++++++
 .../LatinIME/java/res/xml/method.xml          |  2 +-
 .../LatinIME/java/res/xml/rowkeys_brazil2.xml | 30 ++++++++++
 .../java/res/xml/rowkeys_brazil2_left5.xml    | 38 ++++++++++++
 .../java/res/xml/rowkeys_brazil2_right4.xml   | 40 +++++++++++++
 .../LatinIME/java/res/xml/rows_brazil.xml     | 59 +++++++++++++++++++
 .../keyboard/KeyboardLayoutSet.java           |  2 +
 .../inputmethod/latin/utils/ScriptUtils.java  |  8 +++
 11 files changed, 273 insertions(+), 1 deletion(-)
 create mode 100755 packages/inputmethods/LatinIME/java/res/values-pt-rPT/bools.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/kbd_brazil.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/keyboard_layout_set_brazil.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_left5.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_right4.xml
 create mode 100755 packages/inputmethods/LatinIME/java/res/xml/rows_brazil.xml

diff --git a/packages/inputmethods/LatinIME/java/res/values-pt-rPT/bools.xml b/packages/inputmethods/LatinIME/java/res/values-pt-rPT/bools.xml
new file mode 100755
index 00000000000..840d20c2187
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/values-pt-rPT/bools.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+<resources>
+    <!-- Whether this input method should be used as the default for a locale. Override it
+         for supported languages. -->
+    <bool name="im_is_default">true</bool>
+</resources>
diff --git a/packages/inputmethods/LatinIME/java/res/values/attrs.xml b/packages/inputmethods/LatinIME/java/res/values/attrs.xml
index 8ff5a87cd2d..56fa9785791 100644
--- a/packages/inputmethods/LatinIME/java/res/values/attrs.xml
+++ b/packages/inputmethods/LatinIME/java/res/values/attrs.xml
@@ -591,6 +591,7 @@
             <enum name="tamil" value="15" />
             <enum name="telugu" value="16" />
             <enum name="thai" value="17" />
+            <enum name="brazil" value="18" />
         </attr>
     </declare-styleable>
 
diff --git a/packages/inputmethods/LatinIME/java/res/xml/kbd_brazil.xml b/packages/inputmethods/LatinIME/java/res/xml/kbd_brazil.xml
new file mode 100755
index 00000000000..4d405c01b98
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/kbd_brazil.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2011, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<Keyboard
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <include
+        latin:keyboardLayout="@xml/rows_brazil" />
+</Keyboard>
diff --git a/packages/inputmethods/LatinIME/java/res/xml/keyboard_layout_set_brazil.xml b/packages/inputmethods/LatinIME/java/res/xml/keyboard_layout_set_brazil.xml
new file mode 100755
index 00000000000..e0eef3062b3
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/keyboard_layout_set_brazil.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<KeyboardLayoutSet
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin">
+    <Feature
+        latin:supportedScript="brazil" />
+    <Element
+        latin:elementName="alphabet"
+        latin:elementKeyboard="@xml/kbd_brazil"
+        latin:enableProximityCharsCorrection="true" />
+    <Element
+        latin:elementName="symbols"
+        latin:elementKeyboard="@xml/kbd_symbols" />
+    <Element
+        latin:elementName="symbolsShifted"
+        latin:elementKeyboard="@xml/kbd_symbols_shift" />
+    <Element
+        latin:elementName="phone"
+        latin:elementKeyboard="@xml/kbd_phone" />
+    <Element
+        latin:elementName="phoneSymbols"
+        latin:elementKeyboard="@xml/kbd_phone_symbols" />
+    <Element
+        latin:elementName="number"
+        latin:elementKeyboard="@xml/kbd_number" />
+</KeyboardLayoutSet>
diff --git a/packages/inputmethods/LatinIME/java/res/xml/method.xml b/packages/inputmethods/LatinIME/java/res/xml/method.xml
index 3ab25430715..e3a3a98717e 100644
--- a/packages/inputmethods/LatinIME/java/res/xml/method.xml
+++ b/packages/inputmethods/LatinIME/java/res/xml/method.xml
@@ -604,7 +604,7 @@
             android:subtypeId="0xcafff4a6"
             android:imeSubtypeLocale="pt_BR"
             android:imeSubtypeMode="keyboard"
-            android:imeSubtypeExtraValue="KeyboardLayoutSet=qwerty,AsciiCapable,EmojiCapable"
+            android:imeSubtypeExtraValue="KeyboardLayoutSet=brazil,AsciiCapable,EmojiCapable"
             android:isAsciiCapable="true"
     />
     <subtype android:icon="@drawable/ic_ime_switcher_dark"
diff --git a/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2.xml b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2.xml
new file mode 100755
index 00000000000..fc18e830232
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<merge
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <!-- a,s,d,f,g -->
+    <include
+        latin:keyboardLayout="@xml/rowkeys_brazil2_left5" />
+    <!-- h,j,k,l -->
+    <include
+        latin:keyboardLayout="@xml/rowkeys_brazil2_right4" />
+</merge>
diff --git a/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_left5.xml b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_left5.xml
new file mode 100755
index 00000000000..1803bf203e2
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_left5.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2014, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<merge
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <Key
+        latin:keySpec="a"
+        latin:moreKeys="!text/morekeys_a" />
+    <Key
+        latin:keySpec="s"
+        latin:moreKeys="!text/morekeys_s" />
+    <Key
+        latin:keySpec="d"
+        latin:moreKeys="!text/morekeys_d" />
+    <Key
+        latin:keySpec="f" />
+    <Key
+        latin:keySpec="g"
+        latin:moreKeys="!text/morekeys_g" />
+</merge>
diff --git a/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_right4.xml b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_right4.xml
new file mode 100755
index 00000000000..732ec9b7aac
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/rowkeys_brazil2_right4.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2014, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<merge
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <Key
+        latin:keySpec="h"
+        latin:moreKeys="!text/morekeys_h" />
+    <Key
+        latin:keySpec="j"
+        latin:moreKeys="!text/morekeys_j" />
+    <Key
+        latin:keySpec="k"
+        latin:moreKeys="!text/morekeys_k" />
+    <Key
+        latin:keySpec="l"
+        latin:moreKeys="!text/morekeys_l" />
+
+    <!-- U+00BF: "รง" INVERTED QUESTION MARK -->
+    <Key
+        latin:keySpec="&#x00E7;" />
+</merge>
diff --git a/packages/inputmethods/LatinIME/java/res/xml/rows_brazil.xml b/packages/inputmethods/LatinIME/java/res/xml/rows_brazil.xml
new file mode 100755
index 00000000000..efb85c88cda
--- /dev/null
+++ b/packages/inputmethods/LatinIME/java/res/xml/rows_brazil.xml
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2010, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<merge
+    xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
+>
+    <include
+        latin:keyboardLayout="@xml/key_styles_common" />
+    <Row
+        latin:keyWidth="10%p"
+    >
+        <include
+            latin:keyboardLayout="@xml/rowkeys_qwerty1" />
+    </Row>
+    <Row
+        latin:keyWidth="9.0%p"
+    >
+        <include
+            latin:keyboardLayout="@xml/rowkeys_brazil2"
+            />
+        <!--latin:keyXPos="5%p"-->
+        <Key
+            latin:keyStyle="enterKeyStyle"
+            latin:keyWidth="9.0%p" />
+    </Row>
+    <Row
+        latin:keyWidth="10%p"
+    >
+        <Key
+            latin:keyStyle="shiftKeyStyle"
+            latin:keyWidth="15%p"
+            latin:visualInsetsRight="1%p" />
+        <include
+            latin:keyboardLayout="@xml/rowkeys_qwerty3" />
+        <Key
+            latin:keyStyle="deleteKeyStyle"
+            latin:keyWidth="fillRight"
+            latin:visualInsetsLeft="1%p" />
+    </Row>
+    <include
+        latin:keyboardLayout="@xml/row_qwerty4" />
+</merge>
diff --git a/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java b/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
index 26ff051bb47..c6999d9c765 100644
--- a/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
+++ b/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
@@ -393,11 +393,13 @@ public final class KeyboardLayoutSet {
         private static int getXmlId(final Resources resources, final String keyboardLayoutSetName) {
             final String packageName = resources.getResourcePackageName(
                     R.xml.keyboard_layout_set_qwerty);
+            Log.d(TAG, "zwycode: keyboardLayoutSetName="+keyboardLayoutSetName+"---packageName="+packageName);
             return resources.getIdentifier(keyboardLayoutSetName, "xml", packageName);
         }
 
         private void parseKeyboardLayoutSet(final Resources res, final int resId)
                 throws XmlPullParserException, IOException {
+            Log.d(TAG, "zwycode: resId="+resId);
             final XmlResourceParser parser = res.getXml(resId);
             try {
                 while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
diff --git a/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java b/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java
index 537713091a4..494b8874b7d 100644
--- a/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java
+++ b/packages/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/utils/ScriptUtils.java
@@ -16,6 +16,8 @@
 
 package com.android.inputmethod.latin.utils;
 
+import android.util.Log;
+
 import java.util.Locale;
 import java.util.TreeMap;
 
@@ -45,6 +47,7 @@ public class ScriptUtils {
     public static final int SCRIPT_TAMIL = 15;
     public static final int SCRIPT_TELUGU = 16;
     public static final int SCRIPT_THAI = 17;
+    public static final int SCRIPT_HRAZIL = 18;
 
     private static final TreeMap<String, Integer> mLanguageCodeToScriptCode;
 
@@ -68,6 +71,7 @@ public class ScriptUtils {
         mLanguageCodeToScriptCode.put("ta", SCRIPT_TAMIL);
         mLanguageCodeToScriptCode.put("te", SCRIPT_TELUGU);
         mLanguageCodeToScriptCode.put("th", SCRIPT_THAI);
+        mLanguageCodeToScriptCode.put("pt", SCRIPT_HRAZIL);
     }
 
     /*
@@ -79,6 +83,7 @@ public class ScriptUtils {
      * as appropriate, and explicitly excludes CJK, Arabic and Hebrew characters.
      */
     public static boolean isLetterPartOfScript(final int codePoint, final int scriptId) {
+        Log.d("zwycode", "isLetterPartOfScript:   codePoint="+codePoint+"---scriptId="+scriptId);
         switch (scriptId) {
         case SCRIPT_ARABIC:
             // Arabic letters can be in any of the following blocks:
@@ -170,6 +175,9 @@ public class ScriptUtils {
         case SCRIPT_THAI:
             // Thai unicode block is U+0E00..U+0E7F
             return (codePoint >= 0xE00 && codePoint <= 0xE7F);
+        case SCRIPT_HRAZIL:
+            // Thai unicode block is U+0E00..U+0E7F
+            return true;
         case SCRIPT_UNKNOWN:
             return true;
         default:
-- 
2.34.1

### 如何在 AOSP 中启用或切换到中文输入 要在基于 AOSP 的设备上启用并切换到中文输入,通常需要依赖于第三方输入法框架的支持,因为原生 AOSP 并未内置完整的多语言输入解决方案。以下是关于如何配置和使用中文输入的相关信息: #### 配置方法 1. **安装支持中文的输入法框架** 原生 AOSP 不包含像 IBus 这样的复杂输入法框架,因此需要额外集成支持中文输入的服务。例如,在桌面环境中提到的 Wayland 输入协议中,GNOME 使用 Mutter 和 IBus 来处理复杂的 IME 请求[^1]。对于 Android 设备,则可以通过 Google 提供的 LatinIME 或其他开源项目(如 OpenWnn Legacy[^3])扩展其功能。 2. **通过 GBoard 实现中文输入** 自 Android 13 发布以来[^2],Google 官方键盘应用 GBoard 支持多种语言输入,包括简体中文、繁体中文以及拼音等多种输入模式。用户可以在系统设置中的“语言和地区”菜单下添加所需的语言包,并将其设为首选项之一。 3. **利用开源替代方案 Zirco 浏览器或其他工具加载本地化资源文件** 如果偏好完全开放源码环境下的操作体验,可以考虑采用类似于 Zirco Browser 的应用程序来增强整体交互能力[^4]。尽管该浏览器本身并不直接涉及文字录入环节,但它展示了如何构建灵活且高度定制化的移动平台生态体系——同样适用于寻找适合自己的汉字键入途径的过程当中! #### 技术细节补充 为了使上述任一路径生效,可能还需要调整某些底层参数或者重新编译特定模块以适应不同硬件架构的需求。具体而言: - 修改 `InputMethodService` 子类以便识别更多候选词列表; - 更新字典数据集从而提高预测准确性; - 确保图形界面组件能够正确渲染东亚字符集合等等。 ```java // 示例代码片段展示如何动态更改当前激活状态下的软键盘布局样式 public void switchToChineseKeyboard() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (!imm.getCurrentInputMethodSubtype().getLanguage().equals("zh")) { // 判断是否已经是中文 List<InputMethodInfo> imis = imm.getEnabledInputMethodList(); for (InputMethodInfo imi : imis){ if ("com.android.inputmethod.pinyin".equals(imi.getId())){ // 替换为目标插件ID imm.switchToPreviousInputMethod(); break; } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值