Only show number buttons on Soft Keyboard in Android?

笑着哭i 提交于 2019-11-26 17:43:28

问题


On the soft keyboard in Android you can set the soft keyboard to show the numbers instead of a-z keyboard using android:inputType="numberDecimal". 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 @ # $ % ...?

Thanx for listening!


回答1:


You must only add this line on your code:

input.setRawInputType(Configuration.KEYBOARD_12KEY);

this will show only the numeric keyboard.




回答2:


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

is an option




回答3:


The phone number pad is the closest thing I've found (set inputType="phone" on your EditText).




回答4:


The accepted answer did not work for me (tested on OnePlus 3t and Samsung Galaxy S6/S7 phones).

This solution uses numberPassword but overrides the default transformation method for the EditText to show characters instead of showing dots.

<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());



回答5:


Just posted an answer here but re-posting for simplicity:

Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:

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



回答6:


Last I looked into it, Android did not have any good options for that. I ended up having to write my own version of a soft keyboard-like user interface.




回答7:


I had implemented this for android in Xamarin. So, my code is in C#. But the principal stays the same. You can set attribute of edittext to android:inputType="numberPassword".

Then within your code you attach custom transformation method to your edittext view.

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();
        }
    }



回答8:


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

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


来源:https://stackoverflow.com/questions/3357302/only-show-number-buttons-on-soft-keyboard-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!