Android的自定义数字键盘 [英] Android custom numeric keyboard

查看:152
本文介绍了Android的自定义数字键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想补充的数字键盘像在Vault应用

  

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

解决方案
  1. 使用TableLayout创建数字键盘布局。
  2. 绑定View.OnClickListener对每个自定义键,以便响应用户的输入。
  3. 在回应,追加或删除文字,它实现了由EditText.You可以使用附加()或的setText()来控制将被填充在密码字段密码字段。

键盘布局code是这样的:

 < XML版本=1.0编码=UTF-8&GT?;

< TableLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / anti_theft_t9_grid
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:背景=@色/ white_grey
    机器人:分隔=@机器人:彩色/ darker_gray
    机器人:方向=垂直
    机器人:showDividers =中间|开始|结束>

<的TableRow
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:分隔=@机器人:彩色/ darker_gray
    机器人:重力=中心
    机器人:showDividers =中间>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_1
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ NUMBER_ONE
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_2
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ NUMBER_TWO
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_3
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_three
        机器人:textIsSelectable =FALSE/>
< /的TableRow>

<的TableRow
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:分隔=@机器人:彩色/ darker_gray
    机器人:重力=中心
    机器人:showDividers =中间>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_4
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_four
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_5
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_five
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_6
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_six
        机器人:textIsSelectable =FALSE/>
< /的TableRow>

<的TableRow
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:分隔=@机器人:彩色/ darker_gray
    机器人:重力=中心
    机器人:showDividers =中间>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_7
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_seven
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_8
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_eight
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_9
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ number_nine
        机器人:textIsSelectable =FALSE/>
< /的TableRow>

<的TableRow
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:分隔=@机器人:彩色/ darker_gray
    机器人:重力=中心
    机器人:showDividers =中间>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_clear
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ anti_theft_keyboard_clear
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium
        机器人:textIsSelectable =FALSE/>

    <的TextView
        机器人:ID =@ + ID / anti_theft_t9_key_0
        风格=@风格/ anti_theft_t9_key
        机器人:文本=@字符串/ NUMBER_ZERO
        机器人:textIsSelectable =FALSE/>

    < ImageView的
        机器人:ID =@ + ID / anti_theft_t9_key_backspace
        风格=@风格/ anti_theft_t9_key
        机器人:contentDescription =@字符串/ app_name_for_anti_theft
        机器人:SRC =@可绘制/ anti_theft_keyboard_backspace
        机器人:textIsSelectable =FALSE/>
< /的TableRow>

< / TableLayout>
 

每个键的风格:

 <样式名称=anti_theft_t9_key>
    <项目名称=机器人:layout_width> 0dp< /项目>
    <项目名称=机器人:layout_height> WRAP_CONTENT< /项目>
    <项目名称=机器人:layout_weight> 1< /项目>
    <项目名称=机器人:paddingTop> @扪/ anti_theft_t9_key_paddingTop< /项目>
    <项目名称=机器人:paddingBottom会> @扪/ anti_theft_t9_key_paddingBottom< /项目>
    <项目名称=机器人:可点击>真< /项目>
    <项目名称=安卓重力>中心< /项目>
    <项目名称=机器人:scaleType> centerInside< /项目>
    <项目名称=机器人:背景> @可绘制/ anti_theft_btn_blue_bg< /项目>
    <项目名称=?安卓textAppearance>机器人:ATTR / textAppearanceLarge< /项目>
< /风格>
 

每个键的反应:

 私人的EditText mEtPassword;

私人无效setViews(){
    //查找视图参考...
    //设置OnClickListener每个键查看...
}

私人无效onT9KeyClicked(INT键){
    开关(钥匙){
    案例R.id.anti_theft_t9_key_0:
        mEtPassword.append(0);
        打破;
    案例R.id.anti_theft_t9_key_1:
        mEtPassword.append(1);
        打破;
    案例R.id.anti_theft_t9_key_2:
        mEtPassword.append(2);
        打破;
    案例R.id.anti_theft_t9_key_3:
        mEtPassword.append(3);
        打破;
    案例R.id.anti_theft_t9_key_4:
        mEtPassword.append(4);
        打破;
    案例R.id.anti_theft_t9_key_5:
        mEtPassword.append(5);
        打破;
    案例R.id.anti_theft_t9_key_6:
        mEtPassword.append(6);
        打破;
    案例R.id.anti_theft_t9_key_7:
        mEtPassword.append(7);
        打破;
    案例R.id.anti_theft_t9_key_8:
        mEtPassword.append(8);
        打破;
    案例R.id.anti_theft_t9_key_9:
        mEtPassword.append(9);
        打破;
    案例R.id.anti_theft_t9_key_backspace:{
        //删除一个字符
        。字符串passwordStr = mEtPassword.getText()的toString();
        如果(passwordStr.length()大于0){
            字符串newPasswordStr =新的StringBuilder(passwordStr)
                    .deleteCharAt(passwordStr.length() -  1)的ToString();
            mEtPassword.setText(newPasswordStr);
        }
    }
        打破;
    案例R.id.anti_theft_t9_key_clear:
        //清除密码字段
        mEtPassword.setText(空);
        打破;
    }
}
 

I want to add numeric keyboard like the one in vault application

I don't know how to call it and how can I find in google ?

解决方案

  1. Use TableLayout to create the numeric keyboard layout.
  2. Bind View.OnClickListener on each custom key view to response user input.
  3. In responses, append or delete text to that password field which implements by EditText.You can use append() or setText() to control what will be filled in the password field.

The keyboard layout code looks like this:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/anti_theft_t9_grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white_grey"
    android:divider="@android:color/darker_gray"
    android:orientation="vertical"
    android:showDividers="middle|beginning|end" >

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:gravity="center"
    android:showDividers="middle" >

    <TextView
        android:id="@+id/anti_theft_t9_key_1"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_one"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_2"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_two"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_3"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_three"
        android:textIsSelectable="false" />
</TableRow>

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:gravity="center"
    android:showDividers="middle" >

    <TextView
        android:id="@+id/anti_theft_t9_key_4"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_four"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_5"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_five"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_6"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_six"
        android:textIsSelectable="false" />
</TableRow>

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:gravity="center"
    android:showDividers="middle" >

    <TextView
        android:id="@+id/anti_theft_t9_key_7"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_seven"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_8"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_eight"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_9"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_nine"
        android:textIsSelectable="false" />
</TableRow>

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:divider="@android:color/darker_gray"
    android:gravity="center"
    android:showDividers="middle" >

    <TextView
        android:id="@+id/anti_theft_t9_key_clear"
        style="@style/anti_theft_t9_key"
        android:text="@string/anti_theft_keyboard_clear"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/anti_theft_t9_key_0"
        style="@style/anti_theft_t9_key"
        android:text="@string/number_zero"
        android:textIsSelectable="false" />

    <ImageView
        android:id="@+id/anti_theft_t9_key_backspace"
        style="@style/anti_theft_t9_key"
        android:contentDescription="@string/app_name_for_anti_theft"
        android:src="@drawable/anti_theft_keyboard_backspace"
        android:textIsSelectable="false" />
</TableRow>

</TableLayout>

Each key style:

<style name="anti_theft_t9_key">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1</item>
    <item name="android:paddingTop">@dimen/anti_theft_t9_key_paddingTop</item>
    <item name="android:paddingBottom">@dimen/anti_theft_t9_key_paddingBottom</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center</item>
    <item name="android:scaleType">centerInside</item>
    <item name="android:background">@drawable/anti_theft_btn_blue_bg</item>
    <item name="android:textAppearance">?android:attr/textAppearanceLarge</item>
</style>

Responses of each key:

private EditText mEtPassword ;

private void setViews(){
    // find view references...
    // set OnClickListener to each key view...
}

private void onT9KeyClicked(int key) {
    switch (key) {
    case R.id.anti_theft_t9_key_0:
        mEtPassword.append("0");
        break;
    case R.id.anti_theft_t9_key_1:
        mEtPassword.append("1");
        break;
    case R.id.anti_theft_t9_key_2:
        mEtPassword.append("2");
        break;
    case R.id.anti_theft_t9_key_3:
        mEtPassword.append("3");
        break;
    case R.id.anti_theft_t9_key_4:
        mEtPassword.append("4");
        break;
    case R.id.anti_theft_t9_key_5:
        mEtPassword.append("5");
        break;
    case R.id.anti_theft_t9_key_6:
        mEtPassword.append("6");
        break;
    case R.id.anti_theft_t9_key_7:
        mEtPassword.append("7");
        break;
    case R.id.anti_theft_t9_key_8:
        mEtPassword.append("8");
        break;
    case R.id.anti_theft_t9_key_9:
        mEtPassword.append("9");
        break;
    case R.id.anti_theft_t9_key_backspace: {
        // delete one character
        String passwordStr = mEtPassword.getText().toString();
        if (passwordStr.length() > 0) {
            String newPasswordStr = new StringBuilder(passwordStr)
                    .deleteCharAt(passwordStr.length() - 1).toString();
            mEtPassword.setText(newPasswordStr);
        }
    }
        break;
    case R.id.anti_theft_t9_key_clear:
        // clear password field
        mEtPassword.setText(null);
        break;
    }
}

这篇关于Android的自定义数字键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆