如何在 android 中的 EditText 上显示数字键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1119583/
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
How do I show the number keyboard on an EditText in android?
提问by Chiwai Chan
I just basically want to switch to the number pad mode as soon a certain EditText has the focus.
我只是想在某个 EditText 获得焦点后立即切换到数字键盘模式。
回答by Josef Pfleger
回答by Dominic
To do it in a Java file:
要在 Java 文件中执行此操作:
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
回答by Matthijs
I found this implementation useful, shows a better keyboard and limits input chars.
我发现这个实现很有用,显示了更好的键盘并限制了输入字符。
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
Additionally, you could use android:maxLength
to limit the max amount of numbers.
此外,您可以使用android:maxLength
来限制最大数量。
To do this programmatically:
要以编程方式执行此操作:
editText.setInputType(InputType.TYPE_CLASS_PHONE);
KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);
回答by Nguyen Minh Binh
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:inputType="number|phone"/>
will show the large number pad as dialer.
将大数字键盘显示为拨号器。
回答by Nirav Ranpara
You can do it 2 ways
您可以通过 2 种方式进行
Runtime set
EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_NUMBER);
Using XML
<EditText ... android:inputType="number" />
运行时集
EditText input = new EditText(this); input.setInputType(InputType.TYPE_CLASS_NUMBER);
使用 XML
<EditText ... android:inputType="number" />
回答by macio.Jun
If you need fractional number, then this is the answer for you:
如果您需要小数,那么这就是您的答案:
android:inputType="numberDecimal"
回答by Pir Fahim Shah
If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc
如果您在对话框中使用 EditText 或动态创建并且您无法从 xml 设置它,那么此示例将帮助您在使用动态编辑文本等时设置该类型的键盘
myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
where myEditTxt is the dynamic EDIT TEXT object(name)
其中 myEditTxt 是动态 EDIT TEXT 对象(名称)
回答by Aliya
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
回答by Akhila Madari
Below code will only allow numbers "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.
下面的代码只允许数字“0123456789”,即使你不小心输入了“0123456789”以外的数字,编辑文本也不会接受。
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));