如何检查 "textfield1" < "textfield2"

how to check if "textfield1" < "textfield2"

我想检查一个已填充的文本字段是否大于另一个已填充的文本字段。 所以喜欢:

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();

我找不到方法

我假设您的 TextView 中有数字?从 String 中创建一个 Integer 并比较这些数字:

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()) {
} 

试试这个

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
    }
}