简体   繁体   English

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

[英]How to set separate validation for each EditText in android?

I have 3 EditText view i my android project.我的 android 项目中有 3 个 EditText 视图。 I've set validation for all EditText and they display a Toast when any one of them is empty.我已经为所有 EditText 设置了验证,当其中任何一个为空时,它们会显示一个 Toast。 What i want is, to set different toast for every EditText.我想要的是,为每个 EditText 设置不同的吐司。 For example if first EditText is empty it should say first is empty and so on.例如,如果第一个 EditText 是空的,它应该说 first 是空的,依此类推。

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

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

There is no other way but to check each and every EditText .除了检查每一个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;注意:需要使用return; if you want to stop further validation of inputs.如果您想停止进一步验证输入。 Otherwise, remove return;否则,去掉return; and use else if statement.并使用else if语句。

Simple make a function whih a edittext argument Example:简单地制作一个带有edittext参数的 function 示例:

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

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.或 setError() 与 Toast 的组合。

You can pass list of EditTexts and show toast and return false if any of them is empty.您可以传递 EditTexts 列表并显示 toast,如果其中任何一个为空,则返回 false。

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 + "is empty!", Toast.LENGTH_LONG);
            return false;
        }
    }
    return true;
}

You can call above function by您可以通过以下方式致电 function

Boolean allNotEmpty = validateEditTexts(new List<EditText>(view.findViewById(R.id.tvOne), view.findViewById(R.id.tvTwo)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM