相信很多情况下我们看到过一些数字的输入,弹出来的并不是系统自带的键盘。这就是自定义的软键盘,软键盘的一个好处就是简单,操作方便。如何实现一个自定义的软键盘呢??其实这个过程是比较简单的,只要把几个关键的原理搞明白了,你就会发现真的很简单,很方便!看一下效果图:
这篇博客主要介绍一下实现的相关原理,下一节就会把具体实现的步骤和大家分享一下!
实现软键盘主要用到了系统的两个类Keyboard和KeyboardView:
Keyboard类源码的介绍是: Listener for virtual keyboard events.即用于监听虚拟键盘。
至于Keyboard类的映射机制,这里就不需要说了,本人感觉也没必要了解的太深,这里要介绍一些他,在接下来需要的几个重要的内容。我们看一下他的源码:
package com.android.inputmethod.keyboard;
import android.util.Log;
import android.util.SparseArray;
import com.android.inputmethod.keyboard.internal.KeyVisualAttributes;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.keyboard.internal.KeyboardParams;
import com.android.inputmethod.latin.CollectionUtils;
/**
* Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
* consists of rows of keys.
* <p>The layout file for a keyboard contains XML that looks like the following snippet:</p>
* <pre>
* <Keyboard
* latin:keyWidth="%10p"
* latin:keyHeight="50px"
* latin:horizontalGap="2px"
* latin:verticalGap="2px" >
* <Row latin:keyWidth="32px" >
* <Key latin:keyLabel="A" />
* ...
* </Row>
* ...
* </Keyboard>
* </pre>
*/
public class Keyboard {
private static final String TAG = Keyboard.class.getSimpleName();
/** Some common keys code. Must be positive.
* These should be aligned with values/keycodes.xml
*/
public static final int CODE_ENTER = '\n';
public static final int CODE_TAB = '\t';
public static final int CODE_SPACE = ' ';
public static final int CODE_PERIOD = '.';
public static final int CODE_DASH = '-';
public static final int CODE_SINGLE_QUOTE = '\'';
public static final int CODE_DOUBLE_QUOTE = '"';
public static final int CODE_QUESTION_MARK = '?';
public static final int CODE_EXCLAMATION_MARK = '!';
// TODO: Check how this should work for right-to-left languages. It seems to stand
// that for rtl languages, a closing parenthesis is a left parenthesis. Is this
// managed by the font? Or is it a different char?
public static final int CODE_CLOSING_PARENTHESIS = ')';
public static final int CODE_CLOSING_SQUARE_BRACKET = ']';
public static final int CODE_CLOSING_CURLY_BRACKET = '}';
public static final int CODE_CLOSING_ANGLE_BRACKET = '>';
/** Special keys code. Must be negative.
* These should be aligned with KeyboardCodesSet.ID_TO_NAME[],
* KeyboardCodesSet.DEFAULT[] and KeyboardCodesSet.RTL[]
*/
public static final int CODE_SHIFT = -1;
public static final int CODE_SWITCH_ALPHA_SYMBOL = -2;
public static final int CODE_OUTPUT_TEXT = -3;
public static final int CODE_DELETE = -4;
public static final int CODE_SETTINGS = -5;
public static final int CODE_SHORTCUT = -6;
public static final int CODE_ACTION_ENTER = -7;
public static final int CODE_ACTION_NEXT = -8;
public static final int CODE_ACTION_PREVIOUS = -9;
public static final int CODE_LANGUAGE_SWITCH = -10;
public static final int CODE_RESEARCH = -11;
// Code value representing the code is not specified.
public static final int CODE_UNSPECIFIED = -12;
public final KeyboardId mId;
public final int mThemeId;
/** Total height of the keyboard, including the padding and keys */
public final int mOccupiedHeight;
/** Total width of the keyboard, including the padding and keys */
public final int mOccupiedWidth;
/** The padding above the keyboard */
public final int mTopPadding;
/** Default gap between rows */
public final int mVerticalGap;
/** Per keyboard key visual parameters */
public final KeyVisualAttributes mKeyVisualAttributes;
public final int mMostCommonKeyHeight;
public final int mMostCommonKeyWidth;
/** More keys keyboard template */
public final int mMoreKeysTemplate;
/** Maximum column for more keys keyboard */
public final int mMaxMoreKeysKeyboardColumn;
/** Array of keys and icons in this keyboard */
public final Key[] mKeys;
public final Key[] mShiftKeys;
public final Key[] mAltCodeKeysWhileTyping;
public final KeyboardIconsSet mIconsSet;
private final SparseArray<Key> mKeyCache = CollectionUtils.newSparseArray();
private final ProximityInfo mProximityInfo;
private final boolean mProximityCharsCorrectionEnabled;
public Keyboard(final KeyboardParams params) {
/// M: @{
ProximityInfo tmp;
/// @}
mId = params.mId;
mThemeId = params.mThemeId;
mOccupiedHeight = params.mOccupiedHeight;
mOccupiedWidth = params.mOccupiedWidth;
mMostCommonKeyHeight = params.mMostCommonKeyHeight;
mMostCommonKeyWidth = params.mMostCommonKeyWidth;
mMoreKeysTemplate = params.mMoreKeysTemplate;
mMaxMoreKeysKeyboardColumn = params.mMaxM
补充:移动开发 , Android ,