Android自定義輸入法軟鍵盤
本文實例為大傢分享瞭Android自定義輸入法軟鍵盤的具體代碼,供大傢參考,具體內容如下
1 功能描述
- 觸屏設備主界面中有一個文本編輯框,底部區域固定顯示一個數字鍵盤,鍵盤中除數字鍵外,還帶有*和#鍵功能;
- 提供一個自定義的數字輸入法,生成apk安裝包文件,嵌入到img鏡像文件中去。
2 設計實現
1、創建類名為SimpleIME,繼承父類InputMethodService,實現KeyboardView.OnKeyboardActionListener接口。
2、編寫鍵盤對象加載的.xml文件。
3、重寫onCreateInputView()方法,初始化鍵盤視圖和創建鍵盤對象,使用鍵盤視圖對象設置鍵盤的監聽。
4、重寫onKey、onPress、onRelease、onText、swipeDown、swipeLeft、swipeRight和swipeUp等方法,在onKey方法中可處理對鍵盤的操作,在這個方法裡通過傳入的primaryCode進行相應的操作。其他方法沒有具體的實現。
5、配置清單文件的聲明service,還向系統申請瞭IME的BIND_INPUT_METHOD權限,並且給IME添加瞭一個名稱為android.view.InputMethod的過濾器,用來存放意圖的屬性。
3 貼出代碼
1、SimpleIME.java
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.media.AudioManager; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.InputConnection; /** * @description: 自定義輸入法 * @version: v1.0 * @author: yeyl * @date: 2018/6/26 14:57 * @history: */ public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener { private KeyboardView mKeyboardView; private Keyboard mKeyboard; /** * 大小寫轉換的flag */ private boolean mCaps = false; @Override public View onCreateInputView() { mKeyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.layout_keyboard, null); mKeyboard = new Keyboard(this, R.xml.keyboard_number); mKeyboardView.setKeyboard(mKeyboard); mKeyboardView.setOnKeyboardActionListener(this); return mKeyboardView; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { return super.onKeyDown(keyCode, event); } @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_SHIFT: // 大小寫切換 mCaps = !mCaps; mKeyboard.setShifted(mCaps); mKeyboardView.invalidateAllKeys(); break; case Keyboard.KEYCODE_DONE: // 完成 ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER)); break; case Keyboard.KEYCODE_CANCEL: // 取消 ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); break; default: char code = (char) primaryCode; if (Character.isLetter(code) && mCaps) { code = Character.toUpperCase(code); } ic.commitText(String.valueOf(code), 1); } } /** * 播放按鍵音 * * @param keyCode 鍵碼 */ private void playClick(int keyCode) { 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 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() { } }
2、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/layout_preview" />
3、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:background="#FFFF00" android:gravity="center" android:textSize="30sp" android:textStyle="bold" />
4、keyboard_number.xml
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:horizontalGap="0px" android:keyWidth="33.3%p" android:keyHeight="9%p" android:verticalGap="0px"> <Row> <Key android:codes="49" android:keyEdgeFlags="left" android:keyLabel="1" /> <Key android:codes="50" android:keyLabel="2" /> <Key android:codes="51" android:keyEdgeFlags="right" android:keyLabel="3" /> </Row> <Row> <Key android:codes="52" android:keyEdgeFlags="left" android:keyLabel="4" /> <Key android:codes="53" android:keyLabel="5" /> <Key android:codes="54" android:keyEdgeFlags="right" android:keyLabel="6" /> </Row> <Row> <Key android:codes="55" android:keyEdgeFlags="left" android:keyLabel="7" /> <Key android:codes="56" android:keyLabel="8" /> <Key android:codes="57" android:keyEdgeFlags="right" android:keyLabel="9" /> </Row> <Row android:rowEdgeFlags="bottom"> <Key android:codes="-3" android:keyEdgeFlags="left" android:keyLabel="*" /> <Key android:codes="48" android:keyLabel="0" /> <Key android:codes="-4" android:isRepeatable="true" android:keyEdgeFlags="right" android:keyLabel="完成" /> </Row> </Keyboard>
5、AndroidManifest.xml
<service android:name=".SimpleIME" android:label="@string/simple_ime" android:permission="android.permission.BIND_INPUT_METHOD"> <meta-data android:name="android.view.im" android:resource="@xml/method" /> <intent-filter> <action android:name="android.view.InputMethod" /> </intent-filter> </service>
6、method.xml
<?xml version="1.0" encoding="utf-8"?> <input-method xmlns:android="http://schemas.android.com/apk/res/android"> <subtype android:imeSubtypeLocale="en_US" android:imeSubtypeMode="keyboard" android:label="@string/subtype_en_US" /> </input-method>
4 Demo截圖
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。