如何为Android中的每个EditText设置单独的验证?

huangapple go评论90阅读模式
英文:

How to set separate validation for each EditText in android?

问题

我在我的Android项目中有3个EditText视图。我已为所有EditText设置了验证,当它们中的任何一个为空时,它们会显示一个Toast。我想要的是为每个EditText设置不同的Toast。例如,如果第一个EditText为空,它应该显示“第一个为空”,依此类推。

这是我的代码:

if(firstValue.getText().toString().isEmpty() | secondValue.getText().toString().isEmpty() | thirdValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "请输入所有字段!", Toast.LENGTH_SHORT).show();
}

(Note: The code provided is in English, so it doesn't need translation.)

英文:

I have 3 EditText view i my android project. I've set validation for all EditText and they display a Toast when any one of them is empty. What i want is, to set different toast for every EditText. For example if first EditText is empty it should say first is empty and so on.

Here is my code :

if(firstValue.getText().toString().isEmpty() | secondValue.getText().toString().isEmpty() | thirdValue.getText().toString().isEmpty()) {
Toast.makeText(mainactivity.this, "Please enter all fields!", Toast.LENGTH_SHORT).show();
}

答案1

得分: 3

我猜这是个好方法。试试这个。

if(firstValue.getText().toString().isEmpty()){
    setToast("firstValue message");
    return;
}
if(secondValue.getText().toString().isEmpty()){
    setToast("secondValue message");
    return;
}
if(thirdValue.getText().toString().isEmpty()){
    setToast("thirdValue message");
    return;
}

创建方法。

public void setToast(String msg){
    Toast.makeText(activity.this,msg,Toast.LENGTH_SHORT).show();
}
英文:

I guess this is good way. Try this

    if(firstValue.getText().toString().isEmpty()){
        setToast("firstValue message");
        return;
    }
    if(secondValue.getText().toString().isEmpty()){
        setToast("secondValue message");
        return;
    }
    if(thirdValue.getText().toString().isEmpty()){
        setToast("thirdValue message");
        return;
    }

Create method

	public void setToast(String msg){
		Toast.makeText(activity.this,msg,Toast.LENGTH_SHORT).show();
	}

答案2

得分: 2

只有检查每个EditText的方法。可以以许多不同的方式实现它,但最简单的方式是:

if (firstValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "请在第一个字段中输入值!", Toast.LENGTH_SHORT).show();
    return;
}

if (secondValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "请在第二个字段中输入值!", Toast.LENGTH_SHORT).show();
    return;
}

if (thirdValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "请在第三个字段中输入值!", Toast.LENGTH_SHORT).show();
    return;
}

注意:如果要停止进一步的输入验证,需要使用 return;。否则,移除 return; 并使用 else if 语句。

英文:

There is no other way but to check each and every EditText. You can implement it in many different ways but the most simple is:

if(firstValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "Please enter value into the first field!", Toast.LENGTH_SHORT).show();
    return;
}

if(secondValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "Please enter value into the second field!", Toast.LENGTH_SHORT).show();
    return;
}

if(thirdValue.getText().toString().isEmpty()) {
    Toast.makeText(mainactivity.this, "Please enter value into the third field!", Toast.LENGTH_SHORT).show();
    return;
}

Note: you need to use return; if you want to stop further validation of inputs. Otherwise, remove return; and use else if statement.

答案3

得分: 1

创建一个接受 EditText 参数的函数示例:

void validateEditText(EditText editText){
    if (editText.getText().toString().isEmpty()){
        if (editText == firstValue){
            Toast.makeText(mainactivity.this, "第一个为空!", Toast.LENGTH_SHORT).show();
        } else if (editText == secondValue){
            Toast.makeText(mainactivity.this, "第二个为空!", Toast.LENGTH_SHORT).show();
        }
    }
}
英文:

Simple make a function whih a edittext argument Example :

void validateEditText(EditText editText){
    if (editText.getText().toString().isEmpty()){
        if (editText==firstValue){
            Toast.makeText(mainactivity.this, "First Empty!", Toast.LENGTH_SHORT).show();
        }else if (editText==secondValue){
            Toast.makeText(mainactivity.this, "Second Empty!", Toast.LENGTH_SHORT).show();
        }
    }
}

答案4

得分: 1

我个人喜欢在同一个编辑框中显示图标,以显示错误:

if (firstValue.getText().toString().isEmpty()) {
    firstValue.setError("请输入第一个字段的值!");
    return;
}

if (secondValue.getText().toString().isEmpty()) {
    secondValue.setError("请输入第二个字段的值!");
    return;
}

if (thirdValue.getText().toString().isEmpty()) {
    thirdValue.setError("请输入第三个字段的值!");
    return;
}

或者结合使用setError()和Toast。

英文:

I personally like an icon to appear in the same edittext that throws the error:

if(firstValue.getText().toString().isEmpty()) {
    firstValue.setError("Please enter value into the first field!");
    return;
}

if(secondValue.getText().toString().isEmpty()) {
    secondValue.setError("Please enter value into the second field!");
    return;
}

if(thirdValue.getText().toString().isEmpty()) {
    thirdValue.setError("Please enter value into the third field!");
    return;
}

Or a combination of setError() with Toast.

答案5

得分: 1

您可以传递EditText的列表如果其中任何一个为空则显示toast并返回false

```java
Boolean validateEditTexts(List<EditText> editTexts) {
    for (EditText editText : editTexts) {
        if (editText.getText().toString().isEmpty()) {
            String name = editText.getResources().getResourceEntryName(editText.getId());
            Toast.makeText(this, name + "为空!", Toast.LENGTH_LONG).show();
            return false;
        }
    }
    return true;
}

您可以通过以下方式调用上述函数:

Boolean allNotEmpty = validateEditTexts(Arrays.asList(view.findViewById(R.id.tvOne), view.findViewById(R.id.tvTwo)));
英文:

You can pass list of EditTexts and show toast and return false if any of them is empty.

Boolean validateEditTexts(List&lt;EditText&gt; editTexts) {
    for(EditText editText: editTexts) {
        if(editText.getText().toString().isEmpty()) {
            String name = editText.getResources().getResourceEntryName(editText.getId());
            Toast.makeText(this, name + &quot;is empty!&quot;, Toast.LENGTH_LONG);
            return false;
        }
    }
    return true;
}

You can call above function by

Boolean allNotEmpty = validateEditTexts(new List&lt;EditText&gt;(view.findViewById(R.id.tvOne), view.findViewById(R.id.tvTwo)))

huangapple
  • 本文由 发表于 2020年7月30日 18:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63170888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定