Android 13 LatinIME 原生输入法一键关闭功能
在安卓13定制中,需要在原生输入法添加按键关闭输入法。需要在LatinME中增加一个虚拟按键并且设置点击事件来实现关闭功能,如下图。
代码实现细节
1.在KeyboardCodesSet中添加按键值
diff --git a/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java b/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
index 05b4c7473..81b6d828a 100644
--- a/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
+++ b/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardCodesSet.java
@@ -53,6 +53,7 @@ public final class KeyboardCodesSet {
"key_emoji",
"key_alpha_from_emoji",
"key_unspecified",
+ "key_hide",
};
private static final int[] DEFAULT = {
@@ -73,6 +74,7 @@ public final class KeyboardCodesSet {
Constants.CODE_EMOJI,
Constants.CODE_ALPHA_FROM_EMOJI,
Constants.CODE_UNSPECIFIED,
+ Constants.CODE_HIDE,
};
2.在KeyboardIconsSet在设置按钮的背景布局并与之匹配
diff --git a/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
old mode 100644
new mode 100755
index 15a5bd456..ada0a4f32
--- a/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
+++ b/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java
@@ -58,6 +58,7 @@ public final class KeyboardIconsSet {
public static final String NAME_ZWJ_KEY = "zwj_key";
public static final String NAME_EMOJI_ACTION_KEY = "emoji_action_key";
public static final String NAME_EMOJI_NORMAL_KEY = "emoji_normal_key";
+ public static final String NAME_HIDE_KEY = "hide_key";
private static final SparseIntArray ATTR_ID_TO_ICON_ID = new SparseIntArray();
@@ -87,6 +88,7 @@ public final class KeyboardIconsSet {
NAME_ZWJ_KEY, R.styleable.Keyboard_iconZwjKey,
NAME_EMOJI_ACTION_KEY, R.styleable.Keyboard_iconEmojiActionKey,
NAME_EMOJI_NORMAL_KEY, R.styleable.Keyboard_iconEmojiNormalKey,
+ NAME_HIDE_KEY, R.styleable.Keyboard_iconHideKey,
};
3.在LatinIME中设置点击事件再调用方法中的requestHideSelf(0);关闭输入法
diff --git a/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java b/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java
old mode 100644
new mode 100755
index 8ed3a59bf..244c63db0
--- a/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -110,6 +110,7 @@ import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import android.util.Log;
/**
* Input method implementation for Qwerty'ish keyboard.
@@ -2015,4 +2016,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
visible ? Color.BLACK : Color.TRANSPARENT);
}
}
+ @Override
+ public void onCloseInputMethod() {
+ requestHideSelf(0);
+ Log.d("tag","hide onclick");
+ }
}
4.在你想要加的地方布局初始化按钮,这边我是加在建议条(左上角)位置的。
diff --git a/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
old mode 100644
new mode 100755
index 2c80858b5..93c3fabb9
--- a/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
+++ b/inputmethods/LatinIME/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java
@@ -61,6 +61,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
public void showImportantNoticeContents();
public void pickSuggestionManually(SuggestedWordInfo word);
public void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat);
+ public void onCloseInputMethod();
}
static final boolean DBG = DebugFlags.DEBUG_ENABLED;
@@ -68,6 +69,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
private final ViewGroup mSuggestionsStrip;
private final ImageButton mVoiceKey;
+ private final ImageButton mHideKey;
private final View mImportantNoticeStrip;
MainKeyboardView mMainKeyboardView;
@@ -140,6 +142,7 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
mSuggestionsStrip = (ViewGroup)findViewById(R.id.suggestions_strip);
mVoiceKey = (ImageButton)findViewById(R.id.suggestions_strip_voice_key);
mImportantNoticeStrip);
@@ -175,9 +178,12 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
final TypedArray keyboardAttr = context.obtainStyledAttributes(attrs,
R.styleable.Keyboard, defStyle, R.style.SuggestionStripView);
final Drawable iconVoice = keyboardAttr.getDrawable(R.styleable.Keyboard_iconShortcutKey);
+ final Drawable iconHide = keyboardAttr.getDrawable(R.styleable.Keyboard_iconHideKey);
keyboardAttr.recycle();
mVoiceKey.setImageDrawable(iconVoice);
mVoiceKey.setOnClickListener(this);
+ mHideKey.setImageDrawable(iconHide);
+ mHideKey.setOnClickListener(this);
}
/**
@@ -457,6 +463,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick
return;
}
+ if (view == mHideKey) {
+ mListener.onCloseInputMethod();
+ return;
+ }
final Object tag = view.getTag();
// {@link Integer} tag is set at
// {@link SuggestionStripLayoutHelper#setupWordViewsTextAndColor(SuggestedWords,int)} and
5.在配置文件中都定义一下按键id
diff --git a/inputmethods/LatinIME/java/res/values/keyboard-icons-holo.xml b/inputmethods/LatinIME/java/res/values/keyboard-icons-holo.xml
old mode 100644
new mode 100755
index f5484bf4e..5105cb457
--- a/inputmethods/LatinIME/java/res/values/keyboard-icons-holo.xml
+++ b/inputmethods/LatinIME/java/res/values/keyboard-icons-holo.xml
@@ -37,5 +37,6 @@
<item name="iconZwjKey">@drawable/sym_keyboard_zwj_holo_dark</item>
<item name="iconEmojiActionKey">@drawable/sym_keyboard_smiley_holo_dark</item>
<item name="iconEmojiNormalKey">@drawable/sym_keyboard_smiley_holo_dark</item>
+ <item name="iconHideKey">@drawable/hide_keyboard</item>
</style>
</resources>
diff --git a/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-dark.xml b/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-dark.xml
old mode 100644
new mode 100755
index 2e2fd0abb..a9bf89ad5
--- a/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-dark.xml
+++ b/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-dark.xml
@@ -42,5 +42,6 @@
<item name="iconZwjKey">@drawable/sym_keyboard_zwj_lxx_dark</item>
<item name="iconEmojiActionKey">@drawable/sym_keyboard_smiley_lxx_dark</item>
<item name="iconEmojiNormalKey">@drawable/sym_keyboard_smiley_lxx_dark</item>
+ <item name="iconHideKey">@drawable/hide_keyboard</item>
</style>
</resources>
diff --git a/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-light.xml b/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-light.xml
old mode 100644
new mode 100755
index 099a706fe..047aec408
--- a/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-light.xml
+++ b/inputmethods/LatinIME/java/res/values/keyboard-icons-lxx-light.xml
@@ -44,5 +44,6 @@
<item name="iconEmojiActionKey">@drawable/sym_keyboard_smiley_lxx_dark</item>
<!-- Use dark green emoji icon (for lxx_light) because a normal key has white color background. -->
<item name="iconEmojiNormalKey">@drawable/sym_keyboard_smiley_lxx_light</item>
+ <item name="iconHideKey">@drawable/hide_keyboard</item>
</style>
</resources>
diff --git a/inputmethods/LatinIME/java/res/values/attrs.xml b/inputmethods/LatinIME/java/res/values/attrs.xml
old mode 100644
new mode 100755
index 8ff5a87cd..bd8dc8476
--- a/inputmethods/LatinIME/java/res/values/attrs.xml
+++ b/inputmethods/LatinIME/java/res/values/attrs.xml
@@ -279,6 +279,7 @@
<attr name="iconImeKey" format="reference" />
<attr name="iconEmojiActionKey" format="reference" />
<attr name="iconEmojiNormalKey" format="reference" />
+ <attr name="iconHideKey" format="reference" />
6.在建议条布局中加入按钮layout布局
diff --git a/inputmethods/LatinIME/java/res/layout/suggestions_strip.xml b/inputmethods/LatinIME/java/res/layout/suggestions_strip.xml
old mode 100644
new mode 100755
index 47e9a918e..d4d605f7c
--- a/inputmethods/LatinIME/java/res/layout/suggestions_strip.xml
+++ b/inputmethods/LatinIME/java/res/layout/suggestions_strip.xml
@@ -35,6 +35,13 @@
We just need to ignore the system's audio and haptic feedback settings. -->
<!-- Provide audio and haptic feedback by ourselves based on the keyboard settings.
We just need to ignore the system's audio and haptic feedback settings. -->
+ <ImageButton
+ android:id="@+id/suggestions_strip_hide_key"
+ android:layout_width="@dimen/config_suggestions_strip_edge_key_width"
+ android:layout_height="fill_parent"
+ android:layout_alignParentStart="true"
+ android:layout_centerVertical="true"
+ android:layout_alignParentLeft="true" />
<LinearLayout
android:id="@+id/important_notice_strip"
设置完成之后就可以看见在右上角一个标记,图标的话要自己找一个添加到drawble文件夹中 ,具体是在packages/inputmethods/LatinIME/java/res/drawable-hdpi/hide_keyboard.png。