我想检查填写的文本字段是否大于填写的其他文本字段。 所以喜欢:
if (textfield1.getText().toString().equals(""))
Toast.makeText(getActivity(), "textfield one is empty, please fill in a number", Toast.LENGTH_SHORT).show();
我想要这样的事情:
if (textfield1.getText().toString().less than textfield2.getText().toString())
Toast.makeText(getActivity(), "textfield one is less than textfield two, this is not allowed", Toast.LENGTH_SHORT).show();
我无法找到
答案 0 :(得分:2)
我假设你的TextViews中有数字?从Integer
创建String
并比较这些数字:
Integer input1 = Integer.parseInt(textfield1.getText().toString());
Integer input2 = Integer.parseInt(textfield2.getText().toString());
if (input1 < input2) { }
如果是输入长度,那么您正在谈论使用String.length()
,如下所示:
if (textfield1.getText().toString().length() < textfield2.getText().toString().length()) {
}
答案 1 :(得分:0)
试试这个
if(textfield1.getText().toString().trim().length() > 0 && textfield2.getText().toString().trim().length() > 0) {
try {
int i1 = Integer.parseInt(textfield1.getText().toString().trim());
int i2 = Integer.parseInt(textfield2.getText().toString().trim());
if(i1 > i2) {
// do needfull here
}
} catch(Exception ex) {
Log.e("tag", ex.getMessage());
// user entered some character which is not number
}
}