android自动软键盘,Android自定义软键盘
MyKeyboard
Android自定義鍵盤的使用
實現步驟
第一步:
1、新建一個xml文件夾放在res目錄下面,然后新建xml文件:money_keyboard.xml
2、然后在XML文件中添加按鈕布局,這個布局就是鍵盤的樣子了
android:horizontalGap="1dp"
android:keyWidth="33.33333%p"
android:keyHeight="10%p"
android:verticalGap="1dp">
android:codes="49"
android:keyLabel="1" />
android:codes="50"
android:keyLabel="2" />
android:codes="51"
android:keyEdgeFlags="right"
android:keyLabel="3" />
android:codes="52"
android:keyLabel="4" />
android:codes="53"
android:keyLabel="5" />
android:codes="54"
android:keyEdgeFlags="right"
android:keyLabel="6" />
android:codes="55"
android:keyLabel="7" />
android:codes="56"
android:keyLabel="8" />
android:codes="57"
android:keyEdgeFlags="right"
android:keyLabel="9" />
android:codes="46"
android:keyLabel="." />
android:codes="48"
android:keyLabel="0" />
android:codes="-5"
android:keyEdgeFlags="right"
android:keyIcon="@drawable/sym_keyboard_delete"
android:keyLabel="aa" />
3 屬性介紹:
Keyboard:
存儲鍵盤以及按鍵相關信息。
android:horizontalGap
按鍵之間默認的水平間距。
android:verticalGap
按鍵之間默認的垂直間距。
android:keyHeight
按鍵的默認高度,以像素或顯示高度的百分比表示。
android:keyWidth:
按鍵的默認寬度,以像素或顯示寬度的百分比表示。
Row:
為包含按鍵的容器。
Key:
用于描述鍵盤中單個鍵的位置和特性。
android:codes
該鍵輸出的unicode值。
android:codes 官網介紹是說這個是該鍵的unicode 值或者逗號分隔值,當然我們也可以設置成我們想要的值,在源碼中提供了幾個特定的值
對照表:
image.png
public static final int KEYCODE_SHIFT = -1;
public static final int KEYCODE_MODE_CHANGE = -2;
public static final int KEYCODE_CANCEL = -3;
public static final int KEYCODE_DONE = -4;
public static final int KEYCODE_DELETE = -5;
public static final int KEYCODE_ALT = -6;
android:isRepeatable
這個屬性如果設置為true,那么當長按該鍵時就會重復接受到該鍵上的動作,在 刪除鍵鍵 和 空格鍵 上通常設為true。
android:keyLabel
顯示在按鍵上的文字。
android:keyIcon與keyLabel
是二選一關系,它會代替文字以圖標的形式顯示在鍵上。
android:keyWidth="33.33333%p"
每一個按鈕的寬度,可以設置百分比
android:keyHeight="10%p"
每一個按鈕高度,可以設置百分比
第二步:布局文件引用KeyBoradView.
android:id="@+id/keyboard_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:background="#d8d8d8"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="@drawable/bg_keyboard_btn"
android:keyTextColor="#333333"
android:paddingTop="1dp"
android:shadowColor="#ffffff"
android:shadowRadius="0.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/et_amount" />
KeyboardView是一個渲染虛擬鍵盤的View。 它處理鍵的渲染和檢測按鍵和觸摸動作。
顯然我們需要KeyboardView來對Keyboard里的數據進行渲染并呈現給我們以及相關的點擊事件做處理。 1)//設置keyboard與KeyboardView相關聯的方法。
public void setKeyboard(Keyboard keyboard)
2)//設置虛擬鍵盤事件的監聽,此方法必須設置,不然會報錯。
public void setOnKeyboardActionListener(OnKeyboardActionListener listener) 步驟上呢,做完第一步的關聯,并設置第二步的事件,調用KeyboardView.setVisible(true);鍵盤就可以顯示出來了, 是不是很簡單。不過到這里還沒有結束哦,接下來我們為了使用上的便利要進行相應的封裝。 封裝 這里我們通過繼承EditText來對Keyboard與KeyboardView進行封裝。
attr.xml文件,這里我們需要通過一個xml類型的自定義屬性引入我們的鍵盤描述文件。
三、實例化KeyBoradView給其設置KeyBorad,以及OnKeyboardActionListener事件監聽。
1、新建一個類,我取名叫KeyUtils然后在里面新建三個屬性。KeyBoard用處可大了,他才是本體,可以通過設置他來切換鍵盤。
2、構造函數,初始下三個參數。
3、先說下預覽圖吧,就是效果圖上的預覽圖,需要預覽圖的話的將setPreviewEnabled設置為true,不過還得在布局文件中的android.inputmethodservice.KeyboardView標簽對立面設置預覽布局。否則,不會有字。至于設置的布局,一個TextView就好了~
onPress: 按下觸發。
onRelease:松開觸發。
onKey : 松開觸發,在OnRelease之前觸發。
swipeLeft : 左滑動,其他同理。哈哈~就這么懶。
onText :需要在 鍵盤xml,也就是我此時的number.xml里面中key標簽對里添加一個
總結
以上是生活随笔為你收集整理的android自动软键盘,Android自定义软键盘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 超好:web app变革之rem
- 下一篇: Android为TV端助力 post带数