仅在 Android 软键盘上显示数字按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3357302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Only show number buttons on Soft Keyboard in Android?
提问by Benny Skogberg
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 @ # $ % ...
?
在 Android 的软键盘上,您可以使用 将软键盘设置为显示数字而不是 az 键盘android:inputType="numberDecimal"
。但是,如果我只想显示顶部数字行1 2 3 4 5 6 7 8 9 0
而不是以 开头的以下行,我该怎么办@ # $ % ...
?
Thanx for listening!
感谢聆听!
回答by softarn
android:inputType="phone"
android:digits="1234567890"
is an option
是一种选择
回答by sergidk
You must only add this line on your code:
您只能在代码中添加这一行:
input.setRawInputType(Configuration.KEYBOARD_12KEY);
this will show onlythe numeric keyboard.
这将仅显示数字键盘。
回答by Justin
The phone number pad is the closest thing I've found (set inputType="phone"
on your EditText
).
电话号码板是我找到的最接近的东西(设置inputType="phone"
在你的EditText
)。
回答by PDP
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());
回答by TopherC
Just posted an answer herebut 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 已经添加了我们正在寻求的功能。这是我用于简单 EditText 数字条目的 xml:
android:inputType="numberPassword"
android:digits="0123456789"
android:singleLine="true"
android:ems="4"
android:textColor="@android:color/black"
android:gravity="center"
回答by Spike Williams
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.
最后我研究了一下,Android 没有任何好的选择。我最终不得不编写自己的类似软键盘的用户界面版本。
回答by Altaf Virani
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"
.
我已经在 Xamarin 中为 android 实现了这个。所以,我的代码是在 C# 中。但校长保持不变。您可以将 edittext 的属性设置为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();
}
}
回答by sgc_code
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()