Android自定义数字键盘

Android自定义数字键盘,android,android-softkeyboard,Android,Android Softkeyboard,我想添加类似vault应用程序中的数字键盘 我不知道怎么称呼它,我怎么能在谷歌上找到它?您可以为此将EditText的inputType设置为number <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10"

我想添加类似vault应用程序中的数字键盘


我不知道怎么称呼它,我怎么能在谷歌上找到它?

您可以为此将EditText的inputType设置为number

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >
        <requestFocus />
    </EditText>

如果您还想设置密码字段,请使用numberPassword

  • 使用TableLayout创建数字键盘布局
  • 在每个自定义键视图上绑定View.OnClickListener以响应用户输入
  • 在响应中,向通过EditText实现的密码字段追加或删除文本。您可以使用append()或setText()控制密码字段中的填充内容
  • 我编写了一个自定义视图,可在任何地方重用,代码如下:

    KeyboardView.java

    public class KeyboardView extends FrameLayout implements View.OnClickListener {
    
        private EditText mPasswordField;
    
        public KeyboardView(Context context) {
            super(context);
            init();
        }
    
        public KeyboardView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            inflate(getContext(), R.layout.keyboard, this);
            initViews();
        }
    
        private void initViews() {
            mPasswordField = $(R.id.password_field);
            $(R.id.t9_key_0).setOnClickListener(this);
            $(R.id.t9_key_1).setOnClickListener(this);
            $(R.id.t9_key_2).setOnClickListener(this);
            $(R.id.t9_key_3).setOnClickListener(this);
            $(R.id.t9_key_4).setOnClickListener(this);
            $(R.id.t9_key_5).setOnClickListener(this);
            $(R.id.t9_key_6).setOnClickListener(this);
            $(R.id.t9_key_7).setOnClickListener(this);
            $(R.id.t9_key_8).setOnClickListener(this);
            $(R.id.t9_key_9).setOnClickListener(this);
            $(R.id.t9_key_clear).setOnClickListener(this);
            $(R.id.t9_key_backspace).setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            // handle number button click
            if (v.getTag() != null && "number_button".equals(v.getTag())) {
                mPasswordField.append(((TextView) v).getText());
                return;
            }
            switch (v.getId()) {
                case R.id.t9_key_clear: { // handle clear button
                    mPasswordField.setText(null);
                }
                break;
                case R.id.t9_key_backspace: { // handle backspace button
                    // delete one character
                    Editable editable = mPasswordField.getText();
                    int charCount = editable.length();
                    if (charCount > 0) {
                        editable.delete(charCount - 1, charCount);
                    }
                }
                break;
            }
        }
    
        public String getInputText() {
            return mPasswordField.getText().toString();
        }
    
        protected <T extends View> T $(@IdRes int id) {
            return (T) super.findViewById(id);
        }
    }
    
    style.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">
    
        <EditText
            android:id="@+id/password_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:background="#eeeeee"
            android:enabled="false"
            android:minHeight="48dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    
        <TableLayout
            android:id="@+id/keyboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:divider="@drawable/keyboard_divider"
            android:orientation="vertical"
            android:showDividers="beginning|middle|end">
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_1"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_one"/>
    
                <TextView
                    android:id="@+id/t9_key_2"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_two"/>
    
                <TextView
                    android:id="@+id/t9_key_3"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_three"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_4"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_four"/>
    
                <TextView
                    android:id="@+id/t9_key_5"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_five"/>
    
                <TextView
                    android:id="@+id/t9_key_6"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_six"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_7"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_seven"/>
    
                <TextView
                    android:id="@+id/t9_key_8"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_eight"/>
    
                <TextView
                    android:id="@+id/t9_key_9"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_nine"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_clear"
                    style="@style/keyboard_button"
                    android:text="@string/btn_clear"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>
    
                <TextView
                    android:id="@+id/t9_key_0"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_zero"/>
    
                <TextView
                    android:id="@+id/t9_key_backspace"
                    style="@style/keyboard_button"
                    android:text="@string/btn_backspace"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>
    
    <style name="keyboard_row">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:divider">@drawable/keyboard_divider</item>
        <item name="android:gravity">center</item>
        <item name="android:showDividers">beginning|middle|end</item>
    </style>
    
    <style name="keyboard_button">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:paddingTop">12dp</item>
        <item name="android:paddingBottom">12dp</item>
        <item name="android:clickable">true</item>
        <item name="android:gravity">center</item>
        <item name="android:scaleType">centerInside</item>
        <item name="android:background">@drawable/keyboard_button_bg</item>
        <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
    </style>
    
    <style name="keyboard_number_button" parent="keyboard_button">
        <item name="android:tag">number_button</item>
    </style>
    
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
        <item android:state_pressed="true">
            <shape>
                <solid android:color="#dddddd"/>
            </shape>
        </item>
        <item android:state_pressed="false">
            <shape>
                <solid android:color="@android:color/transparent"/>
            </shape>
        </item>
    </selector> 
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#dddddd"/>
        <size
            android:width="1px"
            android:height="1px"/>
    </shape>
    
    <string name="number_zero">0</string>
    <string name="number_one">1</string>
    <string name="number_two">2</string>
    <string name="number_three">3</string>
    <string name="number_four">4</string>
    <string name="number_five">5</string>
    <string name="number_six">6</string>
    <string name="number_seven">7</string>
    <string name="number_eight">8</string>
    <string name="number_nine">9</string>
    <string name="btn_clear">Clear</string>
    <string name="btn_backspace">Back</string>
    
    可绘制的
    keyboard\u divider.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">
    
        <EditText
            android:id="@+id/password_field"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:background="#eeeeee"
            android:enabled="false"
            android:minHeight="48dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    
        <TableLayout
            android:id="@+id/keyboard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:divider="@drawable/keyboard_divider"
            android:orientation="vertical"
            android:showDividers="beginning|middle|end">
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_1"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_one"/>
    
                <TextView
                    android:id="@+id/t9_key_2"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_two"/>
    
                <TextView
                    android:id="@+id/t9_key_3"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_three"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_4"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_four"/>
    
                <TextView
                    android:id="@+id/t9_key_5"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_five"/>
    
                <TextView
                    android:id="@+id/t9_key_6"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_six"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_7"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_seven"/>
    
                <TextView
                    android:id="@+id/t9_key_8"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_eight"/>
    
                <TextView
                    android:id="@+id/t9_key_9"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_nine"/>
            </TableRow>
    
            <TableRow style="@style/keyboard_row">
    
                <TextView
                    android:id="@+id/t9_key_clear"
                    style="@style/keyboard_button"
                    android:text="@string/btn_clear"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>
    
                <TextView
                    android:id="@+id/t9_key_0"
                    style="@style/keyboard_number_button"
                    android:text="@string/number_zero"/>
    
                <TextView
                    android:id="@+id/t9_key_backspace"
                    style="@style/keyboard_button"
                    android:text="@string/btn_backspace"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>
    
    <style name="keyboard_row">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:divider">@drawable/keyboard_divider</item>
        <item name="android:gravity">center</item>
        <item name="android:showDividers">beginning|middle|end</item>
    </style>
    
    <style name="keyboard_button">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:paddingTop">12dp</item>
        <item name="android:paddingBottom">12dp</item>
        <item name="android:clickable">true</item>
        <item name="android:gravity">center</item>
        <item name="android:scaleType">centerInside</item>
        <item name="android:background">@drawable/keyboard_button_bg</item>
        <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
    </style>
    
    <style name="keyboard_number_button" parent="keyboard_button">
        <item name="android:tag">number_button</item>
    </style>
    
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_shortAnimTime">
        <item android:state_pressed="true">
            <shape>
                <solid android:color="#dddddd"/>
            </shape>
        </item>
        <item android:state_pressed="false">
            <shape>
                <solid android:color="@android:color/transparent"/>
            </shape>
        </item>
    </selector> 
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#dddddd"/>
        <size
            android:width="1px"
            android:height="1px"/>
    </shape>
    
    <string name="number_zero">0</string>
    <string name="number_one">1</string>
    <string name="number_two">2</string>
    <string name="number_three">3</string>
    <string name="number_four">4</string>
    <string name="number_five">5</string>
    <string name="number_six">6</string>
    <string name="number_seven">7</string>
    <string name="number_eight">8</string>
    <string name="number_nine">9</string>
    <string name="btn_clear">Clear</string>
    <string name="btn_backspace">Back</string>
    
    在布局中使用自定义
    键盘视图

    <com.xxx.yyy.KeyboardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    github上有一个开源库可以解决您的确切问题:

    请访问获取examplehttps://github.com/rciovati/Android-KeyboardView-ExampleYes,但我想定制键盘的外观。为此,您需要按照AvatarQing在回答中的建议编写自己的键盘布局。根据您喜欢的颜色、背景编写绘图。充气布局并将其设置为键盘视图。请参考这个。希望这有帮助。如果您需要详细信息,请告诉我。如果不按问题显示,请打开默认数字键盘ID。您可以实现我没有发布在此处的字符串、可绘图项、尺寸和颜色吗?或者您可以显示一些代码片段吗?我不知道您面临的问题。我想提出一个懒惰的建议。将onClick(视图v)设置为所有关键点。然后检查此键是否为文本视图(如果v instanceof TextView),要追加文本,只需使用mTextView.append(((TextView)v.getText());这样,您就不需要多次写入同一行。@NiiLaryea创建一个新的自定义视图,将布局从xml扩展到构造函数中的新视图,然后查找并设置每个视图,添加一些公共方法以公开给外部调用,以获取一些有用的数据。最后在anywhere中重新使用此视图。@NiiLaryea我为您编写了一个自定义视图,请检查更新的答案。@AREWGoodQ我定义了一个名为
    getInputText()
    的方法,该方法返回密码字段的文本。或者,您可以定义自己的方法,该方法返回密码\字段TextView实例。