Android 鍵盤開發(fā)知識(shí)點(diǎn)總結(jié)
先廢話一下,說說開發(fā)鍵盤的原因:像理財(cái)產(chǎn)品、銀行等app客戶端登錄時(shí),尤其是要輸入密碼時(shí),會(huì)屏蔽掉系統(tǒng)默認(rèn)輸入法,改為自己的輸入法!這個(gè)是考慮安全,以及防止被輸入法軟件記錄密碼等問題!所以,安全性極高的app都會(huì)要求密碼等都用自己的輸入法,這就有開發(fā)的需求 了!
言歸正傳:開發(fā)這種軟件盤,從什么地方開始著手呢?
步驟1:
先看Android給我們提供的Demo
關(guān)于軟鍵盤的Demo,在以下目錄中能找到:
..\samples\android-22\legacy\SoftKeyboard
步驟二:鍵盤布局
從Demo中可以看出,鍵盤的開發(fā)和界面開發(fā)不一樣,雖然鍵盤也需要布局,但是卻不是用的布局文件,而是在xml目錄里的文件
先來看個(gè):
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的文件里進(jìn)行的,每一行以< Row>開始和結(jié)束,鍵則是以< key>為起始節(jié)點(diǎn),而鍵盤是監(jiān)聽鍵的數(shù)字碼為主要監(jiān)聽對(duì)象的,label 只是鍵盤的顯示標(biāo)簽;
2> 而Keyboard 節(jié)點(diǎn)里的屬性android:keyWidth=”10%p” 是指:如果鍵key的節(jié)點(diǎn)里沒有該屬性,則寬度為 整個(gè)屏幕寬度的10%,如果key的節(jié)點(diǎn)里有該屬性,則以key的節(jié)點(diǎn)屬性為最終值;
3>key節(jié)點(diǎn)屬性里android:codes=”46,44” ,codes為兩個(gè),意思是:第一次點(diǎn)擊是46的字符串,第二次點(diǎn)擊是44的字符串,兩次點(diǎn)擊相隔一秒的時(shí)間;
步驟三:分析代碼
鍵盤組件是繼承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設(shè)置監(jiān)聽事件
設(shè)置監(jiān)聽事件setOnKeyboardActionListener,實(shí)現(xiàn)onKey的方法,
步驟五:EditText使用場(chǎng)景布局
在使用指定輸入法的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>開發(fā)鍵盤時(shí),遇到以下問題:
點(diǎn)擊的Popup,字體都是白色的,有時(shí)是黑色的,和主題有關(guān)系,解決方法:
KeyboardView有一個(gè)屬性,keyPreviewLayout,即是預(yù)覽鍵盤的布局文件,可以自己定義,以TextView 為布局文件的根節(jié)點(diǎn)
2>預(yù)覽布局文件的Popup 高度太高,如何調(diào)整,想調(diào)整成方形的:
KeyboardView有一個(gè)屬性keyPreviewHeight,即是預(yù)覽額高度,即可以調(diào)整
相關(guān)文章
Android利用ViewPager實(shí)現(xiàn)可滑動(dòng)放大縮小畫廊效果
這篇文章主要介紹了Android利用ViewPager實(shí)現(xiàn)可滑動(dòng)放大縮小畫廊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android關(guān)于WebView中無法定位的問題解決
本篇文章主要介紹了Android關(guān)于WebView中無法定位的問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Android編程實(shí)現(xiàn)WebView添加進(jìn)度條的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)WebView添加進(jìn)度條的方法,涉及Android WebView界面及控件功能相關(guān)操作技巧,需要的朋友可以參考下2017-02-02一款A(yù)ndroid APK的結(jié)構(gòu)構(gòu)成解析
本篇文章介紹了我在學(xué)習(xí)過程中對(duì)于Android 程序的理解總結(jié),刨析了apk的組成與產(chǎn)生過程,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-10-10android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn)
這篇文章主要介紹了android H5本地緩存加載優(yōu)化的實(shí)戰(zhàn),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android 5.1 WebView內(nèi)存泄漏問題及快速解決方法
下面小編就為大家?guī)硪黄狝ndroid 5.1 WebView內(nèi)存泄漏問題及快速解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05