如何在Android上将EditText键盘设置为仅包含数字? [英] How do you set the EditText keyboard to only consist of numbers on Android?

查看:260
本文介绍了如何在Android上将EditText键盘设置为仅包含数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的EditText显示一个键盘,该键盘具有可见数字,没有其他字符.

I want my EditText to display a keyboard that ONLY has numbers visible, no other characters.

我已经用所有可用的输入进行了测试,但是它不起作用.我在寻找一种获取只有数字的键盘的方法,但是我只看到了对以下内容的引用:

I have tested with all available inputs and it doesn't work. I searched for a way to get a keyboard that only has numbers but I have only seen references to:

android: inputType = "numberPassword"

但是我希望数字可见,例如:(numberPassword)

But I want the numbers to be visible, like this: (numberPassword)

我尝试过:

android:digits="0123456789"
android:inputType="phone"

android:inputType="number"

但它看起来像这样:

推荐答案

经过几次尝试,我明白了! 我通过编程方式设置键盘值,如下所示:

After several tries, I got it! I'm setting the keyboard values programmatically like this:

myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

或者,如果需要,您可以像这样编辑XML:

Or if you want you can edit the XML like so:

android: inputType = "numberPassword"

这两个配置都会显示密码项目符号,因此我们需要创建一个自定义的ClickableSpan类:

Both configs will display password bullets, so we need to create a custom ClickableSpan class:

private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return source;
    }
}

最后,我们需要在EditText上实现它,以便显示键入的字符.

Finally we need to implement it on the EditText in order to display the characters typed.

myEditText.setTransformationMethod(new NumericKeyBoardTransformationMethod());

这是我的键盘现在的样子:

This is how my keyboard looks like now:

这篇关于如何在Android上将EditText键盘设置为仅包含数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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