想要在值为 NULL 或 VOID 时显示错误消息(如果 EditText 为空但应用程序不断崩溃)
Want to display an error message if the value is NULL or VOID if EditText is empty but apps keeps crashing
public void add(View v)
{
EditText first=findViewById(R.id.first),second=findViewById(R.id.second);
double f=Double.parseDouble(first.getText().toString());
double s=Double.parseDouble(second.getText().toString());
TextView result=findViewById(R.id.result);
double r;
if(TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if(TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
r = f + s;
result.setText("" + r);
}
}
我想从用户输入中添加两个数字,并在 editText 为空时显示错误消息。
但是在执行这段代码时,我的应用程序一直崩溃。
如果 Editext
值不为空,您需要将 Editext
值转换为 Double
试试这个
public void add(View v)
{
EditText first=findViewById(R.id.first);
EditText second=findViewById(R.id.second);
TextView result=findViewById(R.id.result);
double r;
if(TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if(TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
double s=Double.parseDouble(second.getText().toString());
double f=Double.parseDouble(first.getText().toString());
r = f + s;
result.setText("" + r);
}
}
- 添加"null"校验,空校验前
例如:
if((first.gettext().toString) == null ||
TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if((second.gettext().toString) == null || TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
r = f + s;
result.setText("" + r);
}
全球第一,第二
public void add(View v) {
first = findViewById(R.id.first);
second = findViewById(R.id.second);
TextView result = findViewById(R.id.result);
double r;
if (Validates()) {
double s = Double.parseDouble(second.getText().toString());
double f = Double.parseDouble(first.getText().toString());
r = f + s;
result.setText("" + r);
}
}
public boolean Validates() {
if (first.getText().toString().equalsIgnoreCase("")) {
first.setError("This field can't be empty");
return false;
} else if (second.getText().toString().equalsIgnoreCase("")) {
second.setError("This field can't be empty");
return false;
} else {
return true;
}
}
public void add(View v)
{
EditText first=findViewById(R.id.first),second=findViewById(R.id.second);
double f=Double.parseDouble(first.getText().toString());
double s=Double.parseDouble(second.getText().toString());
TextView result=findViewById(R.id.result);
double r;
if(TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if(TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
r = f + s;
result.setText("" + r);
}
}
我想从用户输入中添加两个数字,并在 editText 为空时显示错误消息。
但是在执行这段代码时,我的应用程序一直崩溃。
如果 Editext
值不为空,您需要将 Editext
值转换为 Double
试试这个
public void add(View v)
{
EditText first=findViewById(R.id.first);
EditText second=findViewById(R.id.second);
TextView result=findViewById(R.id.result);
double r;
if(TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if(TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
double s=Double.parseDouble(second.getText().toString());
double f=Double.parseDouble(first.getText().toString());
r = f + s;
result.setText("" + r);
}
}
- 添加"null"校验,空校验前
例如:
if((first.gettext().toString) == null ||
TextUtils.isEmpty(first.getText().toString()))
{
first.setError("This field can't be empty");
}
else if((second.gettext().toString) == null || TextUtils.isEmpty(second.getText().toString()))
{
second.setError("This field can't be empty");
}
else {
r = f + s;
result.setText("" + r);
}
全球第一,第二
public void add(View v) {
first = findViewById(R.id.first);
second = findViewById(R.id.second);
TextView result = findViewById(R.id.result);
double r;
if (Validates()) {
double s = Double.parseDouble(second.getText().toString());
double f = Double.parseDouble(first.getText().toString());
r = f + s;
result.setText("" + r);
}
}
public boolean Validates() {
if (first.getText().toString().equalsIgnoreCase("")) {
first.setError("This field can't be empty");
return false;
} else if (second.getText().toString().equalsIgnoreCase("")) {
second.setError("This field can't be empty");
return false;
} else {
return true;
}
}