2016-12-05 123 views
1

我需要創建一個只包含數字的自定義鍵盤。
但我發現的所有教程都是關於普通鍵盤(QWERTY)的。如何使用數字創建自定義Android鍵盤?

我需要這樣的佈局,但我不知道該從哪裏開始。

enter image description here

請注意,這只是用我的editTexts不會做numbersphone輸入法。

+1

或者您也可以在PIN編輯文本下方製作自己的按鈕和佈局。 – HendraWD

+2

「請注意,僅對我的editText使用數字或手機輸入法不會執行」 - 創建自定義輸入法編輯器不會對您有所幫助,因爲用戶不必使用自定義輸入法編輯器鍵入到你的「EditText」小部件中。聽起來這不應該是一個'EditText',但是,正如HendraWD所建議的那樣,只是一組按鈕,你可以用它來填充一個'TextView'作爲視覺反饋。 – CommonsWare

+1

http://stackoverflow.com/a/21873135/3887432 –

回答

0

我現在沒有使用鍵盤,而是使用GridLayout代替。

<GridLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentStart="true" 
    android:layout_gravity="center" 
    android:columnCount="3" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="1" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="2" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="3" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="4" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="5" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="6" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="7" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="8" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:text="9" /> 

    <Button 
     android:layout_columnWeight="1" 
     android:layout_column="1" 
     android:text="0" /> 
</GridLayout> 
1

我沒有它基本上喜歡這個教程:

https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615

但有了這個

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android" 
android:keyWidth="23.4375%p" 
android:keyHeight="10%p" > 

<Row> 
    <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left" /> 
    <Key android:codes="50" android:keyLabel="2" /> 
    <Key android:codes="51" android:keyLabel="3" /> 
    <Key android:codes="-5"  android:keyLabel="Del" android:isRepeatable="true" android:horizontalGap="6.25%p" android:keyEdgeFlags="right"/> 
</Row> 
<Row> 
    <Key android:codes="52" android:keyLabel="4" android:keyEdgeFlags="left" /> 
    <Key android:codes="53" android:keyLabel="5" /> 
    <Key android:codes="54" android:keyLabel="6" android:keyEdgeFlags="right"/> 
</Row> 
<Row> 
    <Key android:codes="55" android:keyLabel="7" android:keyEdgeFlags="left" /> 
    <Key android:codes="56" android:keyLabel="8" /> 
    <Key android:codes="57" android:keyLabel="9" android:keyEdgeFlags="right"/> 
    <Key android:codes="-2" android:keyLabel="123" android:horizontalGap="6.25%p"/> 
</Row> 
<Row> 
    <Key android:codes="0" android:keyLabel="ABC" android:keyEdgeFlags="left" /> 
    <Key android:codes="46" android:keyLabel="."/> 
    <Key android:codes="48" android:keyLabel="0" /> 
    <Key android:codes="10" android:keyLabel="Ent" android:horizontalGap="6.25%p" android:keyEdgeFlags="right" /> 
</Row> 

123取代了qwerty.xml代碼和ABC就是在其他鍵盤之間導航。如果不需要,刪除它們。