Android 鍵盤開發心得

來源:互聯網
上載者:User

標籤:android   keyboard   鍵盤   開發   

先廢話一下,說說開發鍵盤的原因:像理財產品、銀行等app用戶端登入時,尤其是要輸入密碼時,會屏蔽掉系統預設IME,改為自己的IME!這個是考慮安全,以及防止被IME軟體記錄密碼等問題!所以,安全性極高的app都會要求密碼等都用自己的IME,這就有開發的需求 了!

言歸正傳:開發這種軟體盤,從什麼地方開始著手呢?

步驟1:

先看Android給我們提供的Demo
關於軟鍵盤的Demo,在以下目錄中能找到:
..\samples\android-22\legacy\SoftKeyboard

步驟二:鍵盤配置

從Demo中可以看出,鍵盤的開發和介面開發不一樣,雖然鍵盤也需要布局,但是卻不是用的布局檔案,而是在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="@dimen/key_height"    >    <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>        <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="-1" android:keyIcon="@drawable/sym_keyboard_shift"                 android:keyWidth="15%p" android:isModifier="true"                android:isSticky="true" 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="-5" android:keyIcon="@drawable/sym_keyboard_delete"                 android:keyWidth="15%p" android:keyEdgeFlags="right"                android:isRepeatable="true"/>    </Row>    <Row android:rowEdgeFlags="bottom">        <Key android:codes="-3" android:keyIcon="@drawable/sym_keyboard_done"                 android:keyWidth="15%p" android:keyEdgeFlags="left"/>        <Key android:codes="-2" android:keyLabel="123" android:keyWidth="10%p"/>        <!--            android:codes: -101 is not a framework-defined key code but a key code that is            privately defined in com.example.android.softkeyboard.LatinKeyboardView.        -->        <Key android:codes="-101" android:keyIcon="@drawable/sym_keyboard_language_switch"                android:keyWidth="10%p"/>        <Key android:codes="32" android:keyIcon="@drawable/sym_keyboard_space"                 android:keyWidth="30%p" android:isRepeatable="true"/>        <Key android:codes="46,44" android:keyLabel=". ,"                android:keyWidth="15%p"/>        <Key android:codes="10" android:keyIcon="@drawable/sym_keyboard_return"                 android:keyWidth="20%p" android:keyEdgeFlags="right"/>    </Row></Keyboard>

分析一下:
1>從以上代碼可以看出,布局主要是在Keyboard的檔案裡進行的,每一行以< Row>開始和結束,鍵則是以< key>為起始節點,而鍵盤是監聽鍵的數字碼為主要監聽對象的,label 只是鍵盤的顯示標籤;
2>
而Keyboard 節點裡的屬性android:keyWidth=”10%p” 是指:如果鍵key的節點裡沒有該屬性,則寬度為 整個螢幕寬度的10%,如果key的節點裡有該屬性,則以key的節點屬性為最終值;
3>key節點屬性裡android:codes=”46,44” ,codes為兩個,意思是:第一次點擊是46的字串,第二次點擊是44的字串,兩次點擊相隔一秒的時間;

步驟三:分析代碼

鍵盤組件是繼承KeyboardView,而自訂的,通過使用Keyboard類載入鍵盤配置檔案,並通過KeyboardView.setKeyboard(Keyboard keyboard)的方法,將布局賦值到View裡;具體如下:
1>使用Keyboard類載入xml檔案:

Keyboard keyboard=new Keyboard(context, R.xml.qwerty);

2>將Keyboard賦值給view,使用KeyboardView裡的方法setKeyboard賦值

 setKeyboard(keyboard);
步驟四 給View設定監聽事件

設定監聽事件setOnKeyboardActionListener,實現onKey的方法,

步驟五:EditText使用情境布局

在使用指定IME的Activity布局裡,添加以下代碼

<RelativeLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content" >        <android.inputmethodservice.KeyboardView            android:id="@+id/keyboard_view"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:focusable="true"            android:focusableInTouchMode="true"            android:background="@color/lightblack"            android:keyBackground="@drawable/btn_keyboard_key"             android:keyTextColor="@color/white"            android:visibility="gone" />    </RelativeLayout>
開發鍵盤時,遇到以下問題:

1>問題:
點擊的Popup,字型都是白色的,有時是黑色的,和主題有關係,解決方案:
KeyboardView有一個屬性,keyPreviewLayout,即是預覽鍵盤的布局檔案,可以自己定義,以TextView 為布局檔案的根節點
2>預覽布局檔案的Popup 高度太高,如何調整,想調整成方形的:
KeyboardView有一個屬性keyPreviewHeight,即是預覽額高度,即可以調整

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android 鍵盤開發心得

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.