身份证号码0-1000的EditText Int号码验证



我正在做作业,我已经独自走了很长的路。我在应用程序中的下一步是*客户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();

有人能解释一下我做错了什么吗?有什么东西我需要进口吗?谢谢

这是因为你的条件语句一开始就不对。

您指定的条件是:

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

希望这能有所帮助!

第页。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();
}

有关"关系运算符"(或条件运算符(的含义的更多信息,请查看本"条件与逻辑"指南。

在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")});

最新更新