简体   繁体   English

Android EditText强制数字键盘但允许非数字字符

[英]Android EditText force numeric keyboard but allow non-numeric chars

I need to maintain an EditText which can be inputted by the user numbers, at some cases I need to set the text of the EditText to something like 12$. 我需要维护一个可以通过用户号输入的EditText ,在某些情况下我需要将EditText的文本设置为12 $。 So I need to be able to setText() to anything I want. 所以我需要能够将setText()为我想要的任何东西。

I've tried setting up an EditText with input type set to number which yields the numeric keypad and also remove the InputFilter 's of that EditText - Which still does not allow me to put non numeric chars 我已经尝试设置一个EditText,输入类型设置为number,产生数字小键盘,同时删除EditText的InputFilter - 这仍然不允许我放置非数字字符

input.setFilters(new InputFilter[] {});

Am I doing something wrong? 难道我做错了什么? Is there a different way to accomplish my goal, an EditText with a numeric keypad that i can programmatically insert some non numeric chars into? 有没有不同的方法来实现我的目标,一个带有数字键盘的EditText,我可以编程方式插入一些非数字字符?

This will keep it numeric but will allow the $ character, add any other special chars you need to the android:digits attribute 这将保持数字,但将允许$字符,添加你需要的任何其他特殊字符到android:digits属性

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:digits="0123456789,$">

Just to add to aviran's answer. 只是为了增加aviran的答案。 If you put any "," in the string then you will also allow commas in the EditText. 如果在字符串中放入任何“,”,那么您还将允许EditText中的逗号。

Below will only allow 0-9 and $. 下面只允许0-9和$。

<EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:digits="0123456789$">

Set input type Phone in xml. 在xml中设置输入类型Phone。

This could help you... 这可以帮到你......

how to set only numeric value for edittext in android? 如何在android中只设置edittext的数值?

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

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