本文演示了如何使用Android Studio编写最简单的输入法。界面和交互非常简单,仅是为了消除肤色,突出了编写Android输入法的要点。

我将输入法命名为AndroidXXIME。
如上一篇文章“在Android下创建输入法”中所述:输入法是包含IME服务的Android应用程序。该服务应首先在程序清单中声明。我的manifest.xml文件如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.binglen.androidxxime"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name=".AndroidXXIME" android:label="@string/xxime" android:permission="android.permission.BIND_INPUT_METHOD" > <intent-filter> <action android:name="android.view.InputMethod" /> intent-filter> <meta-data android:name="android.view.im" android:resource="@xml/method"/> service> application> manifest>
在Android Studio生成的应用程序块的末尾添加IME服务的声明。第一行粗体声明需要BIND_INPUT_METHOD权限,第二行粗体创建一个可以匹配android.view.InputMethod的意图过滤器,第三行粗体定义输入法元数据。
注意:service android:name以后必须与java文件中的类名一致。
下一步,创建服务中声明的资源。
元数据中使用资源xml /方法文件,该文件包含输入法的子类型属性。输入法通过此属性定义输入模式和支持的语言。输入法可以包含多个子类型属性。在项目中的res下创建一个xml文件夹,并将method.xml添加到该文件夹。 method.xml的内容如下:
xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android"> <subtype android:label="@string/subtype_en_US" android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" /> input-method>

有关子类型的属性,请参见InputMethodSubtype:
label是子类型的名称
imeSubtypeLocale是此子类型支持的语言类型
imeSubtypeMode是它支持的模式,可以是键盘或语音。调用输入法时,系统会将用户选择的模式值传递给输入法。
在此处添加上一篇文章中引用的字符串定义:
<string name="xxime">XXIMEstring> <string name="subtype_en_US">English (US)string>
在清单中定义服务的android:label时引用
xxime。此字符串用于在系统“语言和输入法”中显示输入法的名称:


在res / layout /中添加文件keyboard.xml来定义键盘布局,内容如下:
xml version="1.0" encoding="UTF-8"?> <android.inputmethodservice.KeyboardView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/keyboard" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:keyPreviewLayout ="@layout/preview" />
单击android.inputmethodservice.KeyboardView以查看其XML属性,其中keyPreviewLayout表示按下键盘时的布局资源。在res / layout中添加preview.xml,如下所示:
xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#ffff00" android:textStyle="bold" android:textSize="30sp" > TextView>
里面只有一个TextView。
以前的资源引用的源都来自清单文件,但是在任何地方都没有引用keyboard.xml。答案是稍后的答案,它将在AndroidXXIME.java文件(包括下面的qwerty.xml)的onCreateInputView()函数中创建键盘视图和键盘布局时使用。
键信息在键盘中定义,其格式如下:
<Keyboard android:keyWidth="p" android:keyHeight="50px" android:horizontalGap="2px" android:verticalGap="2px" > <Row android:keyWidth="32px" > <Key android:keyLabel="A" /> ... Row> ... Keyboard>
这是一个嵌套结构,其中包含“行”和“键”。每个按钮都有两个必需的属性:

·keyLabel:按钮上显示的文字
·代码:键代表的Unicode代码
我们的关键信息文件位于res / xml / qwerty.xml中,定义如下:
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyWidth="10%p" android:horizontalGap="0px" android:verticalGap="0px" android:keyHeight="60dp" > <Row> <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/> <Key android:codes="119" android:keyLabel="w"/> <Key android:codes="101" android:keyLabel="e"/> <Key android:codes="114" android:keyLabel="r"/> <Key android:codes="116" android:keyLabel="t"/> <Key android:codes="121" android:keyLabel="y"/> <Key android:codes="117" android:keyLabel="u"/> <Key android:codes="105" android:keyLabel="i"/> <Key android:codes="111" android:keyLabel="o"/> <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/> Row> <Row android:layout_centerHorizontal="true"> <Key android:codes="97" android:keyLabel="a" android:horizontalGap="5%p" android:keyEdgeFlags="left"/> <Key android:codes="115" android:keyLabel="s"/> <Key android:codes="100" android:keyLabel="d"/> <Key android:codes="102" android:keyLabel="f"/> <Key android:codes="103" android:keyLabel="g"/> <Key android:codes="104" android:keyLabel="h"/> <Key android:codes="106" android:keyLabel="j"/> <Key android:codes="107" android:keyLabel="k"/> <Key android:codes="108" android:keyLabel="l" android:keyEdgeFlags="right"/> Row> <Row> <Key android:codes="39" android:keyLabel="'" android:keyEdgeFlags="left"/> <Key android:codes="122" android:keyLabel="z"/> <Key android:codes="120" android:keyLabel="x"/> <Key android:codes="99" android:keyLabel="c"/> <Key android:codes="118" android:keyLabel="v"/> <Key android:codes="98" android:keyLabel="b"/> <Key android:codes="110" android:keyLabel="n"/> <Key android:codes="109" android:keyLabel="m"/> <Key android:codes="44" android:keyLabel=","/> <Key android:codes="46" android:keyLabel="." android:keyEdgeFlags="right"/> Row> <Row android:rowEdgeFlags="bottom"> <Key android:codes="63" android:keyLabel="\?" android:keyWidth="10%p" android:keyEdgeFlags="left"/> <Key android:codes="47" android:keyLabel="/" android:keyWidth="10%p" /> <Key android:codes="32" android:keyLabel=" " android:keyWidth="40%p" android:isRepeatable="true"/> <Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="20%p" android:isRepeatable="true"/> <Key android:codes="-4" android:keyLabel="DONE" android:keyWidth="20%p" android:keyEdgeFlags="right"/> Row> Keyboard>
在键盘类中定义了一些负值。
在字母键的定义中:android:horizontalGap =“ 5%p”,官方文档解释说,它用于定义键之间的间距,实际上是到前一个键的距离(如果有的话)是左侧的起点是距左侧边缘的距离。 %p表示父组件中尺寸的比例。
接下来,您需要为输入法创建服务和侦听器。这两个角色可以在一堂课中完成。 AndroidXXIME类扩展了InputMethodService并实现了KeyboardView.OnKeyboardActionListener接口。此类的定义如下:
public class AndroidXXIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView keyboardView; // 对应keyboard.xml中定义的KeyboardView private Keyboard keyboard; // 对应qwerty.xml中定义的Keyboard @Override public void onPress(int primaryCode) { } @Override public void onRelease(int primaryCode) { } @Override public void onText(CharSequence text) { } @Override public void swipeDown() { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeUp() { } @Override public View onCreateInputView() { // keyboard被创建后,将调用onCreateInputView函数 keyboardView = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null); // 此处使用了keyboard.xml keyboard = new Keyboard(this, R.xml.qwerty); // 此处使用了qwerty.xml keyboardView.setKeyboard(keyboard); keyboardView.setOnKeyboardActionListener(this); return keyboardView; } private void playClick(int keyCode){ // 点击按键时播放声音,在onKey函数中被调用 AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE); switch(keyCode){ case 32: am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR); break; case Keyboard.KEYCODE_DONE: case 10: am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN); break; case Keyboard.KEYCODE_DELETE: am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE); break; default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD); } } @Override public void onKey(int primaryCode, int[] keyCodes) { InputConnection ic = getCurrentInputConnection(); playClick(primaryCode); switch(primaryCode){ case Keyboard.KEYCODE_DELETE : ic.deleteSurroundingText(1, 0); break; case Keyboard.KEYCODE_DONE: ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER)); break; default: char code = (char)primaryCode; ic.commitText(String.valueOf(code), 1); } } }

此例程的代码可以在此处找到
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-353316-1.html
大喊被美国人欺负了
这种叫兽不适合高校育人
假货和山寨货横行