EditText Int编号ID编号0-1000的验证

我正在做作业,我已经很孤单了。我在应用程序中的下一步是*客户ID应该在0-1000之间。如果用户输入的值大于1000,则需要显示错误消息。

我不知道该怎么做。我正在观看视频,然后去其他站点查看是否有相似之处,但没有点击。到目前为止,这是我的代码。

String name,address;
    int custID= 0;

    EditText nameInput;
    EditText addressInput;
    EditText custIDInput;

    Button submitButton;



    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    nameInput = this.<EditText>findViewById(R.id.NameeditText);
    addressInput = this.<EditText>findViewById(R.id.addresseditText);
    custIDInput = this.<EditText>findViewById(R.id.CustIDeditText);
    submitButton = this.findViewById(R.id.SubmitButton);


    submitButton.setOnClicklistener(new View.OnClicklistener() {
        @Override
        public void onClick(View v) throws IndexOutOfBoundsException {


            Intent i = new Intent(Mainactivity.this,Customer.class);

            name = nameInput.getText().toString();
            address = addressInput.getText().toString();
            custID = Integer.valueOf(custIDInput.getText().toString());
            if(! custID > 0 && < 1000)
                custIDInput.setError("Error Customer ID: enter number 1-1000");

            i.putExtra("Value1",name);
            i.putExtra("Value2",custID);
            startactivity(i);
            finish();

有人可以解释我在做什么错吗?我需要导入什么东西吗? 谢谢!

fwangeling 回答:EditText Int编号ID编号0-1000的验证

那是因为您的条件语句一开始就不合适。

您指定的条件表明:

  

客户ID应该在0-1000之间。如果用户输入 1000 以上的任何值,则需要显示错误消息。 (强调是我的)

从本质上讲,这意味着仅当用户输入的值大于1000时,才显示错误消息。这将转换为以下(抽象的)代码:

if (input > 1000) {
    // Show your error message here
}

(可选)如果输入有效,您还应该隐藏错误。这样一来,用户在输入有效输入时就不会感到困惑,但仍会显示错误消息。这可以通过else语句完成:

if (input > 1000) {
    // Show your error message here
} else {
    // Hide your error message here or do whatever you want
}

希望这会有所帮助!

P.S。适应当前代码的代码如下所示:

// ...
if (custID > 1000) {
    custIDInput.setError("Please enter a number between 0 and 1000");
} else {
    // Hide the error message
    custIDInput.setError(null);

    i.putExtra("Value1",name);
    i.putExtra("Value2",custID);
    startActivity(i);
    finish();
}

有关“关系运算符”(或条件运算符)的含义的更多信息,请查看此guide from Trinket on "Conditionals and Logic"

,

如下所示在onClick中更改代码。

Intent i = new Intent(MainActivity.this,Customer.class);

    name = nameInput.getText().toString();
    address = addressInput.getText().toString();
    custID = Integer.valueOf(custIDInput.getText().toString());

    if(custID > 0 && custId <= 1000) {
        i.putExtra("Value1",name);
        i.putExtra("Value2",custID);
        startActivity(i);
        finish();
    } else {
       custIDInput.setError("Error Customer ID: enter number 1-1000");
    }
,

首先创建此类:

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

    private int min,max;

    public InputFilterMinMax(int min,int max) {
        this.min = min;
        this.max = max;
    }

    public InputFilterMinMax(String min,String max) {
        this.min = Integer.parseInt(min);
        this.max = Integer.parseInt(max);
    }

    @Override
    public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend) {   
        try {
            int input = Integer.parseInt(dest.toString() + source.toString());
            if (isInRange(min,max,input))
                return null;
        } catch (NumberFormatException nfe) { }     
        return "";
    }

    private boolean isInRange(int a,int b,int c) {
        return b > a ? c >= a && c <= b : c >= b && c <= a;
    }
}

将这行代码添加到您的EditText(XML)

android:inputType="number"

将过滤器设置为Kotlin / Java文件中的editText

et.setFilters(new InputFilter[]{ new InputFilterMinMax("0","1000")});
本文链接:https://www.f2er.com/3162749.html

大家都在问