一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android 鍵盤開發知識點總結

Android 鍵盤開發知識點總結

2022-03-01 15:35Go貝殼 Android

這篇文章我們給大家總結了Android 鍵盤開發的相關知識點內容以及開發心得,有需要的朋友參考學習下。

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

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

步驟1:

先看Android給我們提供的Demo

關于軟鍵盤的Demo,在以下目錄中能找到:

..\samples\android-22\legacy\SoftKeyboard

步驟二:鍵盤布局

從Demo中可以看出,鍵盤的開發和界面開發不一樣,雖然鍵盤也需要布局,但是卻不是用的布局文件,而是在xml目錄里的文件

先來看個:

qwerty.xml文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<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文件:

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

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

?
1
setKeyboard(keyboard);

步驟四 給View設置監聽事件

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

步驟五:EditText使用場景布局

在使用指定輸入法的Activity布局里,添加以下代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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>開發鍵盤時,遇到以下問題:

Android 鍵盤開發知識點總結

點擊的Popup,字體都是白色的,有時是黑色的,和主題有關系,解決方法:

KeyboardView有一個屬性,keyPreviewLayout,即是預覽鍵盤的布局文件,可以自己定義,以TextView 為布局文件的根節點

2>預覽布局文件的Popup 高度太高,如何調整,想調整成方形的:
KeyboardView有一個屬性keyPreviewHeight,即是預覽額高度,即可以調整

原文鏈接:https://blog.csdn.net/zouchengxufei/article/details/47026945

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99人中文字幕亚洲区 | 亚洲成人三级 | 亚洲国产成人久久综合一区 | 欧美影院一区二区三区 | 男女男精品网站免费观看 | ccc在线在线36 | 麻豆性视频 | 97大香伊在人人线色 | 国内外成人在线视频 | chinese野外gay军人 | 国产一久久香蕉国产线看观看 | 免费看a视频 | 欧美人shou交在线播放 | 香蕉精品国产高清自在自线 | 日本又大又硬又粗的视频 | 国产日韩欧美在线一区二区三区 | 日韩欧美a | 情乱奶水欲 | 免费特黄视频 | 九九在线免费视频 | 果冻传媒在线完整免费观 | 911香蕉视频 | 日本在线精品视频 | 国产成人v爽在线免播放观看 | 青青久在线视频免费观看 | 日本特级大片 | 九九精品视频在线观看 | 天若有情1992国语版完整版 | 调教人妖 | 男女被爆动漫羞羞动漫 | 精品亚洲一区二区三区在线播放 | 2021年国内自拍 | 国产精品福利久久2020 | 欧美日韩国产亚洲一区二区 | 无颜之月5集全免费看无删除 | 天天天天天干 | 日产精品一二三四区国产 | 女张腿男人桶羞羞漫画 | 国产色视频网站 | 91制片厂制作果冻传媒破解 | 欧美一级久久久久久久大片 |