简体   繁体   English

仅在 Android 软键盘上显示数字按钮?

[英]Only show number buttons on Soft Keyboard in Android?

On the soft keyboard in Android you can set the soft keyboard to show the numbers instead of az keyboard using android:inputType="numberDecimal" .在 Android 的软键盘上,您可以使用android:inputType="numberDecimal"将软键盘设置为显示数字而不是 az 键盘。 However, what do I do if I only want to show the top number row 1 2 3 4 5 6 7 8 9 0 and not the following rows starting with @ # $ % ... ?但是,如果我只想显示最高数字行1 2 3 4 5 6 7 8 9 0而不是以下以@ # $ % ...开头的行, @ # $ % ...怎么办?

Thanx for listening!感谢聆听!

android:inputType="phone"
android:digits="1234567890"

is an option是一种选择

You must only add this line on your code:您只能在代码中添加这一行:

input.setRawInputType(Configuration.KEYBOARD_12KEY);

this will show only the numeric keyboard.这将显示数字键盘。

电话号码键盘是我发现的最接近的东西(在EditText上设置inputType="phone" )。

The accepted answer did not work for me (tested on OnePlus 3t and Samsung Galaxy S6/S7 phones).接受的答案对我不起作用(在 OnePlus 3t 和三星 Galaxy S6/S7 手机上测试)。

This solution uses numberPassword but overrides the default transformation method for the EditText to show characters instead of showing dots.此解决方案使用 numberPassword 但覆盖 EditText 的默认转换方法以显示字符而不是显示点。

<EditText
    android:id="@+id/userid"
    android:inputType="numberPassword"
    android:maxLength="6"
/>

// Numeric 6 character user id
EditText input = findViewById(R.id.userid);

// Process input and show characters instead of dots
input.setTransformationMethod(SingleLineTransformationMethod.getInstance());

Just posted an answer here but re-posting for simplicity:刚刚在这里发布了一个答案但为简单起见重新发布:

Seems Android has added the functionality we were seeking.似乎 Android 已经添加了我们正在寻求的功能。 This is the xml I use for simple EditText numeric entry:这是我用于简单 EditText 数字条目的 xml:

    android:inputType="numberPassword"
    android:digits="0123456789"
    android:singleLine="true"
    android:ems="4"
    android:textColor="@android:color/black"
    android:gravity="center"

Last I looked into it, Android did not have any good options for that.最后我研究了一下,Android 没有任何好的选择。 I ended up having to write my own version of a soft keyboard-like user interface.我最终不得不编写自己的类似软键盘的用户界面版本。

The accepted answer didn't work for me.接受的答案对我不起作用。 It kept showing other unwanted characters它一直显示其他不需要的字符

I found a way to accomplish the behavior I wanted by changing the inputType to numberPassword and then disabling the password dots我找到了一种方法来完成我想要改变的inputType的行为numberPassword ,然后禁用密码点

textInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
textInput.transformationMethod = SingleLineTransformationMethod()

I had implemented this for android in Xamarin.我已经在 Xamarin 中为 android 实现了这个。 So, my code is in C#.所以,我的代码是在 C# 中。 But the principal stays the same.但校长保持不变。 You can set attribute of edittext to android:inputType="numberPassword" .您可以将 edittext 的属性设置为android:inputType="numberPassword" numberPassword android:inputType="numberPassword"

Then within your code you attach custom transformation method to your edittext view.然后在您的代码中,您将自定义转换方法附加到您的 edittext 视图。

holder.edtxtQty.TransformationMethod = new HiddenPasswordTransformationMethod();

private class HiddenPasswordTransformationMethod : global::Android.Text.Method.PasswordTransformationMethod
        {
            public override Java.Lang.ICharSequence GetTransformationFormatted(Java.Lang.ICharSequence source, View view)
            {
                return new PasswordCharSequence(source);
            }
        }

    private class PasswordCharSequence : Java.Lang.Object, Java.Lang.ICharSequence
    {
        private char DOT = '\u2022';

        private Java.Lang.ICharSequence _source;
        public PasswordCharSequence(Java.Lang.ICharSequence source)
        {
            _source = source;
        }

        public char CharAt(int index)
        {
            return _source.CharAt(index);
        }

        public int Length()
        {
            return _source.Length();
        }

        public Java.Lang.ICharSequence SubSequenceFormatted(int start, int end)
        {
            return _source.SubSequenceFormatted(start, end); // Return default
        }

        public IEnumerator<char> GetEnumerator()
        {
            return _source.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _source.GetEnumerator();
        }
    }

从代码:

et.setInputType(InputType.TYPE_CLASS_NUMBER)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM