Android 如何确定 EditText 中的输入是否为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10120212/
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 to determine if an input in EditText is an integer?
提问by Yansuck
Hi I'm a newbie in Android Programming.
嗨,我是 Android 编程的新手。
I'm trying to build an activity which includes an edittext
field and a button
. When user type in an integer, the button will lead them to the next activity. However I do not if there's a way to check the type of user's input.
我正在尝试构建一个活动,其中包括一个edittext
字段和一个button
. 当用户输入一个整数时,按钮将引导他们进入下一个活动。但是,如果有办法检查用户输入的类型,我不会。
Anyone can help me? Thank you very much!
任何人都可以帮助我吗?非常感谢!
回答by jmaffre
Since API level 1, Android provides an helper method to do just that (no need to use regex or catching exception) : TextUtils.isDigitsOnly(CharSequence str)
从 API 级别 1 开始,Android 提供了一个辅助方法来做到这一点(无需使用正则表达式或捕获异常):TextUtils.isDigitsOnly(CharSequence str)
boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
Note that this method returns truewith empty String : Issue 24965
请注意,此方法返回true并带有空字符串:问题 24965
回答by Mark Pazon
Update:
更新:
You can control the EditText to only accept numbers
您可以控制 EditText 只接受数字
<TextView
.
.
.
android:inputType="number"
/>
or check it programmatically
或以编程方式检查
In Kotlin
在科特林
val number = editText123.text.toString().toIntOrNull()
val isInteger = number != null
In Java
在 Java 中
String text = editText123.getText().toString();
try {
int num = Integer.parseInt(text);
Log.i("",num+" is a number");
} catch (NumberFormatException e) {
Log.i("",text+" is not a number");
}
回答by muffinmad
If you whant EditText accept only numbers you cant specify android:inputType="number"
in layout file.
如果你想让 EditText 只接受你不能android:inputType="number"
在布局文件中指定的数字。
回答by Herry
You can use TextWatcher
for EditText
to get value of every change in EditText
.You need to add interface of TextWatcher in your Activity.
您可以使用TextWatcher
forEditText
来获取每个更改的值EditText
。您需要在您的 Activity 中添加 TextWatcher 的接口。
mEditText.addTextChangedListener(Your Class Name.this);
on in method of TextWatcher
在 TextWatcher 的方法中
@Override
public void afterTextChanged(Editable s) {
Log.v("Log_tag", "After TextChanged" + s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.i("Log_tag", "Before TextChanged" + s.toString());
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("Log_tag", "ontext Changed" + s.toString());
//you can match here s with number or integer
if(isNumeric( s.toString())){
//it is number
}
}
public static boolean isNumeric(String str)
{
return str.matches("-?\d+(.\d+)?");
}
回答by Yuvraj Pandey
Following code can be used to determine digits,
以下代码可用于确定数字,
boolean isDigits = TextUtils.isDigitsOnly(edtDigits.getText().toString());
However since this method returns true with empty String as well so you can put a validation for empty string as follows,
但是,由于此方法也使用空字符串返回 true,因此您可以按如下方式对空字符串进行验证,
public boolean isDigits(String number){
if(!TextUtils.isEmpty(number)){
return TextUtils.isDigitsOnly(number);
}else{
return false;
}
}
回答by SAAM
Try this solution.
试试这个解决方案。
You need to add the properties android:nextFocusDown="@+id/nextedittext_id"
and
android:singleLine="true"
in your edittexts.
您需要添加的属性 android:nextFocusDown="@+id/nextedittext_id"
,并
android:singleLine="true"
在你的edittexts。
For example:
例如:
<EditText
android:id="@+id/ed1"
android:layout_width="200dp"
android:nextFocusDown="@+id/ed2"
android:singleLine="true"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/ed2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_below="@+id/ed1" />
And add the following code in your activity:
并在您的活动中添加以下代码:
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String value=s.toString();
if(isNumeric(value)){
if(Integer.parseInt(value)>=100){
editText1.setFocusableInTouchMode(true);
editText1.setFocusable(false);
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
editText2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
editText1.setFocusableInTouchMode(true);
editText1.setFocusable(true);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
isNumeric method is as same as above:
isNumeric 方法同上:
public static boolean isNumeric(String str)
{
return str.matches("-?\d+(.\d+)?");
}
回答by SAAM
I found a much cleaner solution,which checks if the string is any type of number including integer
, float
, long
or double
.
我发现了一个更清洁溶液,如果字符串是任何类型的号码,包括其检查integer
,float
,long
或double
。
import java.math.BigDecimal;
Boolean isNumber;
try {
BigDecimal n = new BigDecimal(theStingToCheck);
isNumber = true;
} catch (Exception e) {
isNumber = false;
}