概述
轉(zhuǎn)載:http://blog.csdn.net/tianzhaoai/article/details/64130332
突然發(fā)現(xiàn)好多軟件都使用了自己定義的軟鍵盤齐蔽。自己就想著先把這塊坑先踩踩把逾雄,以后掉坑的時(shí)候不至于摔的太慘罪裹。言歸正傳萎羔,對于自定義軟鍵盤构捡。需要用到系統(tǒng)提供的兩個(gè)類:Keyboard和KeyboardView偷俭。
設(shè)計(jì)鍵盤的布局文件浦箱,官方上對Keyboard是這么解釋的
Loads an XML description of a keyboard and stores the attributes of the keys.
A keyboard consists of rows of keys.The layout file for a keyboard contains XML that looks like the following snippet:
也就是說這個(gè)Keyboard是一個(gè)xml文件大渤,可以用來進(jìn)行鍵盤的布局制妄,格式如下:
<Keyboard
android:keyWidth="%10p"
android:keyHeight="50px"
android:horizontalGap="2px"
android:verticalGap="2px" >
<Row android:keyWidth="32px" >
<Key
android:codes="44"
android:keyLabel=","
android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
...
</Row>
...
</Keyboard>
下面是Keyboard描述鍵盤時(shí)的一些常用屬性介紹
屬性 | 描述 |
---|---|
codes | 按鍵對應(yīng)的輸出值,可以為unicode值或則逗號(hào)(,)分割的多個(gè)值泵三,也可以為一個(gè)字 符串耕捞。在字符串中通過“\”來轉(zhuǎn)義特殊字符,例如 ‘\n’ 或則 ‘\uxxxx’ 烫幕。Codes通常用來定義該鍵的鍵碼俺抽,例如上圖中的數(shù)字按鍵1對應(yīng)的為49;如果提供的是逗號(hào)分割的多個(gè)值則和普通手機(jī)輸入鍵盤一樣在多個(gè)值之間切換 |
keyLabel | 按鍵顯示的文本內(nèi)容 |
keyIcon | 按鍵顯示的圖標(biāo)內(nèi)容较曼,如果指定了該值則在顯示的時(shí)候顯示為圖片不顯示文本 |
keyWidth | 按鍵的寬度磷斧,可以為精確值或則相對值,對于精確值支持多種單位捷犹,例如:像素弛饭,英寸 等;相對值為相對于基礎(chǔ)取值的百分比萍歉,為以% 或則%p 結(jié)尾侣颂,其中%p表示相對于父容器 |
keyHeight | 按鍵的高度,取值同keyWidth |
horizontalGap | 按鍵前的間隙(水平方向)枪孩,取值取值同keyWidth |
isSticky | 按鍵是否為sticky的憔晒。例如Shift大小寫切換按鍵藻肄,具有兩種狀態(tài),按下狀態(tài)和正常狀態(tài)拒担,取值為true或則false |
isModifier | 按鍵是否為功能鍵( modifier key ) 嘹屯,例如 Alt 或則 Shift 。取值為true或則false |
keyOutputText | 按鍵輸出的文本內(nèi)容澎蛛,取值為字符串 |
isRepeatable | 按鍵是否是可重復(fù)的抚垄,如果長按該鍵可以觸發(fā)重復(fù)按鍵事件則為true,否則為false |
keyEdgeFlags | 按鍵的對齊指令谋逻,取值為left或則right |
處理繪制呆馁,檢測按鍵,觸摸動(dòng)作等
A view that renders a virtual Keyboard. It handles rendering of keys and detecting key presses and touch movements.
在這里渲染虛擬鍵盤的視圖毁兆,并且處理按鍵的觸摸和移動(dòng)等操作浙滤。在Activity的布局文件中可以這樣定義
<android.inputmethodservice.KeyboardView
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/gray"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/bg_keyboard_selector"
android:keyPreviewLayout="@layout/key_preview_layout"
android:keyTextColor="@color/keyTextColor"
android:visibility="gone"
/>
下面介紹一些KeyboardView的屬性
屬性 | 描述 |
---|---|
android:keyBackground | 鍵的背景圖 |
android:keyPreviewLayout | 擊鍵盤上的某個(gè)鍵時(shí),短暫彈出的提示布局文件 |
android:keyPreviewOffset | 擊鍵盤上的某個(gè)鍵時(shí)气堕,短暫彈出布局的垂直偏移量 |
android:keyTextColor | 按鍵中的keyLabel的顏色 |
android:keyTextSize | 按鍵中的keyLabel的大小 |
android:labelTextSize | 如果有圖片時(shí)纺腊,按鍵中的keyLabel的大小 |
android:popupLayout | 彈出鍵盤的布局文件 |
android:verticalCorrection | 補(bǔ)償觸摸Y坐標(biāo)的偏移量,用于偏移校正 |
基本實(shí)現(xiàn)
一茎芭、定義鍵盤的鍵
自己定義的xml鍵盤布局位于res–>xml文件目錄下
鍵盤上鍵的細(xì)節(jié)和它的位置我們指定在一個(gè)xml文件中揖膜,每一個(gè)鍵都有自己的屬性。
- keyLabel 這個(gè)屬性是指每個(gè)鍵顯示的文本
- codes 這個(gè)屬性是指這個(gè)鍵代表的字符的unicode
比如我們自己定義一個(gè)數(shù)字1梅桩,則他的codes屬性的值是49壹粟,keyLabel屬性的值就是1。
如果一個(gè)code對應(yīng)多個(gè)key宿百,這個(gè)key代表的字符取決于這個(gè)key接受到的點(diǎn)擊數(shù)taps趁仙,例如,一個(gè)鍵具有49垦页,50雀费,51編碼:
- 一次點(diǎn)擊就是 1
- 兩次點(diǎn)擊就是 2
- 三次點(diǎn)擊就是 3
每個(gè)鍵都可以設(shè)置自己的屬性,這里就不一一贅述了痊焊。當(dāng)然盏袄,一般情況下鍵盤上每行的按鍵盡量都控制10個(gè)或10個(gè)以內(nèi),要不然影響用戶體驗(yàn)薄啥,每行都使用進(jìn)行分行貌矿。
一般自己定義的code都為負(fù)數(shù),比如-5代表刪除罪佳,-1代表shift切換等。
數(shù)字鍵盤number.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="40dip"
android:keyWidth="8.33334444%p"
android:verticalGap="6dp">
<Row>
<Key android:codes="49" android:horizontalGap="2dp" android:keyEdgeFlags="left"
android:keyLabel="1"/>
<Key android:codes="50" android:horizontalGap="6dp" android:keyLabel="2"/>
<Key android:codes="51" android:horizontalGap="6dp" android:keyLabel="3"/>
<Key android:codes="52" android:horizontalGap="6dp" android:keyLabel="4"/>
<Key android:codes="53" android:horizontalGap="6dp" android:keyLabel="5"/>
<Key android:codes="54" android:horizontalGap="6dp" android:keyLabel="6"/>
<Key android:codes="55" android:horizontalGap="6dp" android:keyLabel="7"/>
<Key android:codes="56" android:horizontalGap="6dp" android:keyLabel="8"/>
<Key android:codes="57" android:horizontalGap="6dp" android:keyLabel="9"/>
<Key android:codes="48" android:horizontalGap="6dp" android:keyEdgeFlags="right"
android:keyLabel="0"/>
</Row>
<Row>
<Key android:codes="45" android:keyEdgeFlags="left" android:horizontalGap="2dp"
android:keyLabel="-"/>
<Key android:codes="47" android:horizontalGap="6dp" android:keyLabel="/"/>
<Key android:codes="58" android:horizontalGap="6dp" android:keyLabel=":"/>
<Key android:codes="59" android:horizontalGap="6dp" android:keyLabel=";"/>
<Key android:codes="40" android:horizontalGap="6dp" android:keyLabel="("/>
<Key android:codes="41" android:horizontalGap="6dp" android:keyLabel=")"/>
<Key android:codes="165" android:horizontalGap="6dp" android:keyLabel="¥"/>
<Key android:codes="64" android:horizontalGap="6dp" android:keyLabel="\@"/>
<Key android:codes="8220" android:horizontalGap="6dp" android:keyLabel="“"/>
<Key android:codes="8221" android:horizontalGap="6dp" android:keyEdgeFlags="right"
android:keyLabel="”"/>
</Row>
<Row>
<Key android:codes="-7" android:keyEdgeFlags="left" android:horizontalGap="2dp"
android:keyLabel="#+="
android:keyWidth="11.000002%p"/>
<Key android:codes="12290" android:horizontalGap="14dp" android:keyLabel="黑低。"
android:keyWidth="8.33334444%p"/>
<Key android:codes="65292" android:keyLabel="赘艳," android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="65311" android:keyLabel="酌毡?" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="65281" android:keyLabel="!" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="46" android:keyLabel="." android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="96" android:keyLabel="`" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="36" android:keyLabel="$" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="-5"
android:keyEdgeFlags="right"
android:horizontalGap="14dp"
android:keyIcon="@drawable/delete"
android:keyWidth="11.000002%p"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-2" android:keyLabel="abc" android:keyEdgeFlags="left"
android:horizontalGap="2dp"
android:keyWidth="11.000002%p"/>
<Key android:codes="44" android:keyLabel="," android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="-11" android:keyLabel="←" android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="32" android:isRepeatable="true" android:horizontalGap="6dp"
android:keyIcon="@drawable/space"
android:keyWidth="20.999996%p"/>
<Key android:codes="-12" android:horizontalGap="6dp" android:keyLabel="→"
android:keyWidth="8.999998%p"/>
<Key android:codes="46" android:keyLabel="." android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="完成"
android:horizontalGap="6dp"
android:keyWidth="20.000002%p"/>
</Row>
</Keyboard>
英文鍵盤letter.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="40dip"
android:keyWidth="8.33334444%p"
android:verticalGap="6dp">
<Row>
<Key android:codes="113" android:horizontalGap="2dp"
android:keyEdgeFlags="left" android:keyLabel="q"/>
<Key android:codes="119" android:horizontalGap="6dp" android:keyLabel="w"/>
<Key android:codes="101" android:horizontalGap="6dp" android:keyLabel="e"/>
<Key android:codes="114" android:horizontalGap="6dp" android:keyLabel="r"/>
<Key android:codes="116" android:horizontalGap="6dp" android:keyLabel="t"/>
<Key android:codes="121" android:horizontalGap="6dp" android:keyLabel="y"/>
<Key android:codes="117" android:horizontalGap="6dp" android:keyLabel="u"/>
<Key android:codes="105" android:horizontalGap="6dp" android:keyLabel="i"/>
<Key android:codes="111" android:horizontalGap="6dp" android:keyLabel="o"/>
<Key android:codes="112" android:horizontalGap="6dp" android:keyEdgeFlags="right"
android:keyLabel="p"/>
</Row>
<Row>
<Key android:codes="97" android:horizontalGap="4.999995%p"
android:keyEdgeFlags="left" android:keyLabel="a"/>
<Key android:codes="115" android:horizontalGap="6dp" android:keyLabel="s"/>
<Key android:codes="100" android:horizontalGap="6dp" android:keyLabel="d"/>
<Key android:codes="102" android:horizontalGap="6dp" android:keyLabel="f"/>
<Key android:codes="103" android:horizontalGap="6dp" android:keyLabel="g"/>
<Key android:codes="104" android:horizontalGap="6dp" android:keyLabel="h"/>
<Key android:codes="106" android:horizontalGap="6dp" android:keyLabel="j"/>
<Key android:codes="107" android:horizontalGap="6dp" android:keyLabel="k"/>
<Key android:codes="108" android:horizontalGap="6dp"
android:keyEdgeFlags="right" android:keyLabel="l"/>
</Row>
<Row>
<Key android:codes="-1" android:keyEdgeFlags="left" android:horizontalGap="2dp"
android:keyIcon="@drawable/shift" android:keyWidth="11.000002%p"/>
<Key android:codes="122" android:horizontalGap="14dp"
android:keyLabel="z" android:keyWidth="8.33334444%p"/>
<Key android:codes="120" android:keyLabel="x"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="33" android:keyLabel="c"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="118" android:keyLabel="v"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="98" android:keyLabel="b"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="110" android:keyLabel="n"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="109" android:keyLabel="m"
android:horizontalGap="6dp" android:keyWidth="8.33334444%p"/>
<Key android:codes="-5" android:keyEdgeFlags="right" android:horizontalGap="14dp"
android:keyIcon="@drawable/delete" android:keyWidth="11.000002%p"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-2" android:keyLabel="123" android:keyEdgeFlags="left"
android:horizontalGap="2dp" android:keyWidth="11.000002%p"/>
<Key android:codes="44" android:keyLabel=","
android:horizontalGap="6dp" android:keyWidth="8.999998%p"/>
<Key android:codes="-11" android:keyLabel="←"
android:horizontalGap="6dp" android:keyWidth="8.999998%p"/>
<Key android:codes="32" android:isRepeatable="true" android:horizontalGap="6dp"
android:keyIcon="@drawable/space" android:keyWidth="20.999996%p"/>
<Key android:codes="-12" android:horizontalGap="6dp"
android:keyLabel="→" android:keyWidth="8.999998%p"/>
<Key android:codes="46" android:keyLabel="."
android:horizontalGap="6dp" android:keyWidth="8.999998%p"/>
<Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="完成"
android:horizontalGap="6dp" android:keyWidth="20.000002%p"/>
</Row>
</Keyboard>
符號(hào)鍵盤symbol.xml
<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyHeight="40dip"
android:keyWidth="8.33334444%p"
android:verticalGap="6dp">
<Row>
<Key android:codes="12304" android:horizontalGap="2dp" android:keyEdgeFlags="left"
android:keyLabel="【"/>
<Key android:codes="12305" android:horizontalGap="6dp" android:keyLabel="】"/>
<Key android:codes="123" android:horizontalGap="6dp" android:keyLabel="{"/>
<Key android:codes="125" android:horizontalGap="6dp" android:keyLabel="}"/>
<Key android:codes="35" android:horizontalGap="6dp" android:keyLabel="#"/>
<Key android:codes="37" android:horizontalGap="6dp" android:keyLabel="%"/>
<Key android:codes="94" android:horizontalGap="6dp" android:keyLabel="^"/>
<Key android:codes="42" android:horizontalGap="6dp" android:keyLabel="*"/>
<Key android:codes="43" android:horizontalGap="6dp" android:keyLabel="+"/>
<Key android:codes="61" android:horizontalGap="6dp" android:keyEdgeFlags="right"
android:keyLabel="="/>
</Row>
<Row>
<Key android:codes="95" android:keyEdgeFlags="left" android:horizontalGap="2dp"
android:keyLabel="_"/>
<Key android:codes="-9" android:horizontalGap="6dp" android:keyLabel="——"/>
<Key android:codes="92" android:horizontalGap="6dp" android:keyLabel="\"/>
<Key android:codes="124" android:horizontalGap="6dp" android:keyLabel="|"/>
<Key android:codes="126" android:horizontalGap="6dp" android:keyLabel="~"/>
<Key android:codes="12298" android:horizontalGap="6dp" android:keyLabel="《"/>
<Key android:codes="12299" android:horizontalGap="6dp" android:keyLabel="》"/>
<Key android:codes="36" android:horizontalGap="6dp" android:keyLabel="$"/>
<Key android:codes="38" android:horizontalGap="6dp" android:keyLabel="&"/>
<Key android:codes="46" android:horizontalGap="6dp" android:keyEdgeFlags="right"
android:keyLabel="·"/>
</Row>
<Row>
<Key android:codes="-7" android:keyEdgeFlags="left" android:horizontalGap="2dp"
android:keyLabel="123"
android:keyWidth="11.000002%p"/>
<Key android:codes="-8" android:horizontalGap="14dp" android:keyLabel="..."
android:keyWidth="8.33334444%p"/>
<Key android:codes="44" android:keyLabel="," android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="-10" android:keyLabel="^_^" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="63" android:keyLabel="\?" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="33" android:keyLabel="!" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="-13" android:keyLabel="^o^" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="-14" android:keyLabel=">_<" android:horizontalGap="6dp"
android:keyWidth="8.33334444%p"/>
<Key android:codes="-5" android:keyEdgeFlags="right" android:horizontalGap="14dp"
android:keyIcon="@drawable/delete"
android:keyWidth="11.000002%p"/>
</Row>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="-2" android:keyLabel="abc" android:keyEdgeFlags="left"
android:horizontalGap="2dp"
android:keyWidth="11.000002%p"/>
<Key android:codes="44" android:keyLabel="," android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="-11" android:keyLabel="←" android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="32" android:isRepeatable="true" android:horizontalGap="6dp"
android:keyIcon="@drawable/space"
android:keyWidth="20.999996%p"/>
<Key android:codes="-12" android:horizontalGap="6dp" android:keyLabel="→"
android:keyWidth="8.999998%p"/>
<Key android:codes="46" android:keyLabel="." android:horizontalGap="6dp"
android:keyWidth="8.999998%p"/>
<Key android:codes="-3" android:keyEdgeFlags="right" android:keyLabel="完成"
android:horizontalGap="6dp"
android:keyWidth="20.000002%p"/>
</Row>
</Keyboard>
二蕾管、處理按鍵
OnKeyboardActionListener
在KeyboardView類里面有專門處理Keyboard的監(jiān)聽類OnKeyboardActionListener
public class KeyboardView extends View implements View.OnClickListener {
......
/**
* Listener for virtual keyboard events.
*/
public interface OnKeyboardActionListener {
/**
* 當(dāng)用戶按下一個(gè)鍵時(shí)調(diào)用枷踏。 這是在調(diào)用onKey之前。
* 對于重復(fù)的鍵掰曾,此鍵僅調(diào)用一次旭蠕。
* @param primaryCode被按下的鍵的unicode。如果觸摸不在有效范圍內(nèi)旷坦,值將為零掏熬。
*/
void onPress(int primaryCode);
/**
* 當(dāng)用戶釋放鍵時(shí)調(diào)用。 這是在調(diào)用onKey之后秒梅。
* 對于重復(fù)的鍵旗芬,此鍵僅調(diào)用一次。
* @param primaryCode被釋放的鍵的unicode
*/
void onRelease(int primaryCode);
/**
* 發(fā)送一個(gè)按鍵到監(jiān)聽器
* @ param primaryCode這是按下的鍵
* @ param keyCodes所有可能的替代鍵的代碼捆蜀,
*/
void onKey(int primaryCode, int[] keyCodes);
/**
* 向偵聽器發(fā)送一系列字符疮丛。
* @param text 要顯示的字符序列。
*/
void onText(CharSequence text);
/**
* 當(dāng)用戶從右向左快速移動(dòng)手指時(shí)調(diào)用辆它。
*/
void swipeLeft();
/**
* 當(dāng)用戶從左向右快速移動(dòng)手指時(shí)調(diào)用誊薄。
*/
void swipeRight();
/**
* 當(dāng)用戶從上到下快速移動(dòng)手指時(shí)調(diào)用。
*/
void swipeDown();
/**
* 當(dāng)用戶快速將手指從下向上移動(dòng)時(shí)調(diào)用锰茉。
*/
void swipeUp();
}
......
}
我們自己實(shí)現(xiàn)OnKeyboardActionListener接口呢蔫,即可處理對鍵盤的操作。主要實(shí)現(xiàn)了onKey方法洞辣。在這個(gè)方法里通過傳入的primaryCode進(jìn)行相應(yīng)的操作咐刨,如
- 如果code是 KEYCODE_DELETE 使用Editable.delete方法刪除光標(biāo)左邊的字符
- 如果code是 KEYCODE_SHIFT boolean類型的isUpper的值會(huì)被改變,并且使用 setShifted() 方法改變鍵盤的換檔狀態(tài)(shift state)扬霜,當(dāng)狀態(tài)改變時(shí)定鸟,鍵盤需要重繪,所以的鍵的label被更新了著瓶,invalidateAllKeys() 方法用來重繪所有的鍵
- 對于其他所有的codes联予,只是簡單的將unicode轉(zhuǎn)化為字符并且使用Editable.insert()方法插入到輸入框里即可,如果這個(gè)code代表了字母表里的一個(gè)字母材原,并且isUpper變量為true沸久,那么還需要將字母轉(zhuǎn)化為大寫
- 若code為負(fù)數(shù)的話,只需要根據(jù)自己的定義余蟹,進(jìn)行相應(yīng)的處理即可卷胯,例如-8代表省略號(hào),-10代表笑臉等威酒。
public class KeyboardUtil {
...
private static final int SYMBOL_CODE = -7;//符號(hào)鍵盤
private static final int ELLIPSES_CODE = -8;//省略號(hào)
private static final int CHINESE_HORIZONTAL_LINE_CODE = -9;//中文橫線
private static final int SMILING_FACE_CODE = -10;//笑臉
private static final int LEFT_CODE = -11;//中文橫線
private static final int RIGHT_CODE = -12;//中文橫線
private static final int HEE_CODE = -13;//哈哈
private static final int AWKWARD_CODE = -14;//尷尬
OnKeyboardActionListener onKeyboardActionListener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小寫切換
isUpper = !isUpper;
k1.setShifted(isUpper);
keyboardView.invalidateAllKeys();
} else if (primaryCode == SYMBOL_CODE) {// 符號(hào)鍵盤
if (isSymbol) {
isSymbol = false;
keyboardView.setKeyboard(k2);
} else {
isSymbol = true;
keyboardView.setKeyboard(k3);
}
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 數(shù)字鍵盤切換
if (isNum) {
isNum = false;
keyboardView.setKeyboard(k1);
} else {
isNum = true;
keyboardView.setKeyboard(k2);
}
} else if (primaryCode == LEFT_CODE) { //向左
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == RIGHT_CODE) { // 向右
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else if (primaryCode == ELLIPSES_CODE) { //省略號(hào)
editable.insert(start, "...");
} else if (primaryCode == CHINESE_HORIZONTAL_LINE_CODE) {
editable.insert(start, "——");
} else if (primaryCode == SMILING_FACE_CODE) {
editable.insert(start, "^_^");
} else if (primaryCode == HEE_CODE) {
editable.insert(start, "^o^");
} else if (primaryCode == AWKWARD_CODE) {
editable.insert(start, ">_<");
} else {
String str = Character.toString((char) primaryCode);
if (isWord(str)) {
if (isUpper) {
str = str.toUpperCase();
} else {
str = str.toLowerCase();
}
}
editable.insert(start, str);
}
}
};
...
}
KeyboardUtil
這里自定義了一個(gè)鍵盤的工具類KeyboardUtil
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
public class KeyboardUtil {
private KeyboardView keyboardView;
private Keyboard k1;// 字母鍵盤
private Keyboard k2;// 數(shù)字鍵盤
private Keyboard k3;// 符號(hào)鍵盤
private boolean isNum = false;// 是否數(shù)據(jù)鍵盤
private boolean isUpper = false;// 是否大寫
private boolean isSymbol = false;// 是否符號(hào)
private static final int SYMBOL_CODE = -7;//符號(hào)鍵盤
private static final int ELLIPSES_CODE = -8;//省略號(hào)
private static final int CHINESE_HORIZONTAL_LINE_CODE = -9;//中文橫線
private static final int SMILING_FACE_CODE = -10;//笑臉
private static final int LEFT_CODE = -11;//中文橫線
private static final int RIGHT_CODE = -12;//中文橫線
private static final int HEE_CODE = -13;//哈哈
private static final int AWKWARD_CODE = -14;//尷尬
private ViewGroup rootView;
private EditText ed;
private KeyboardUtil(Activity activity, EditText edit) {
this.ed = edit;
k1 = new Keyboard(activity, R.xml.letter);
k2 = new Keyboard(activity, R.xml.number);
k3 = new Keyboard(activity, R.xml.symbol);
keyboardView = new KeyboardView(activity, null);
keyboardView.setKeyboard(k1);
keyboardView.setEnabled(true);
keyboardView.setPreviewEnabled(false);
keyboardView.setOnKeyboardActionListener(onKeyboardActionListener);
rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
}
OnKeyboardActionListener onKeyboardActionListener = new OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = ed.getText();
int start = ed.getSelectionStart();
if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
hideKeyboard();
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {// 大小寫切換
isUpper = !isUpper;
k1.setShifted(isUpper);
keyboardView.invalidateAllKeys();
} else if (primaryCode == SYMBOL_CODE) {// 符號(hào)鍵盤
if (isSymbol) {
isSymbol = false;
keyboardView.setKeyboard(k2);
} else {
isSymbol = true;
keyboardView.setKeyboard(k3);
}
} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {// 數(shù)字鍵盤切換
if (isNum) {
isNum = false;
keyboardView.setKeyboard(k1);
} else {
isNum = true;
keyboardView.setKeyboard(k2);
}
} else if (primaryCode == LEFT_CODE) { //向左
if (start > 0) {
ed.setSelection(start - 1);
}
} else if (primaryCode == RIGHT_CODE) { // 向右
if (start < ed.length()) {
ed.setSelection(start + 1);
}
} else if (primaryCode == ELLIPSES_CODE) { //省略號(hào)
editable.insert(start, "...");
} else if (primaryCode == CHINESE_HORIZONTAL_LINE_CODE) {
editable.insert(start, "——");
} else if (primaryCode == SMILING_FACE_CODE) {
editable.insert(start, "^_^");
} else if (primaryCode == HEE_CODE) {
editable.insert(start, "^o^");
} else if (primaryCode == AWKWARD_CODE) {
editable.insert(start, ">_<");
} else {
String str = Character.toString((char) primaryCode);
if (isWord(str)) {
if (isUpper) {
str = str.toUpperCase();
} else {
str = str.toLowerCase();
}
}
editable.insert(start, str);
}
}
};
private boolean isShow = false;
public void showKeyboard() {
if (!isShow) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
rootView.addView(keyboardView, layoutParams);
isShow = true;
}
}
private void hideKeyboard() {
if (rootView != null && keyboardView != null && isShow) {
isShow = false;
rootView.removeView(keyboardView);
}
mInstance = null;
}
private boolean isWord(String str) {
return str.matches("[a-zA-Z]");
}
private static KeyboardUtil mInstance;
public static KeyboardUtil shared(Activity activity, EditText edit) {
if (mInstance == null) {
mInstance = new KeyboardUtil(activity, edit);
}
mInstance.ed = edit;
return mInstance;
}
}
三窑睁、MainActivity中的使用
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) this.findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
KeyboardUtil.shared(MainActivity.this,edit).showKeyboard();
}
});
}
}
好了挺峡,關(guān)于Android的自定義鍵盤就說到這里,這個(gè)只是自定義鍵盤最簡單的使用方式担钮。當(dāng)然橱赠,就算是最簡單的方式,想要完整的實(shí)現(xiàn)下來也是踩了不少坑的箫津。有時(shí)間了在繼續(xù)踩狭姨。這里貼上源碼下載