Android 验证多个编辑文本

Android 验证多个编辑文本,android,Android,我整晚都在想这个问题,似乎没有一个简单的解决办法。我正在尝试验证我的所有4个字段,以确保每个字段中都有一个值,如果单击“计算”按钮后每个字段中都有一个值,则将计算总数。如果其中任何一个没有值,它将在每个没有值的EditText处返回一个错误,并且不会计算总数 cal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View

我整晚都在想这个问题,似乎没有一个简单的解决办法。我正在尝试验证我的所有4个字段,以确保每个字段中都有一个值,如果单击“计算”按钮后每个字段中都有一个值,则将计算总数。如果其中任何一个没有值,它将在每个没有值的EditText处返回一个错误,并且不会计算总数

cal.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub  
                if( eLoan.getText().toString().length() == 0 )
                {
                    eLoan.setError( "A value is required" );
                }
                else if( eWage.getText().toString().length() == 0 )
                {
                    eWage.setError( "A value is required" );
                }
                else if( eGrant.getText().toString().length() == 0 )
                {
                    eGrant.setError( "A value is required" );
                }
                else if( eOther.getText().toString().length() == 0 )
                {
                    eOther.setError( "A value is required" );
                }
                else
                convertToString();
                converToDouble();               
                inTotal = inLoan + inWage + inGrant + inOther;
                DecimalFormat currency = new DecimalFormat(".00");
                TotalInNum.setText("£" + currency.format(inTotal));
            }

        });
我无法理解它,我试图合并一个布尔语句来检查每个EditText,但它也不起作用。我相信有一种更简单的方法可以做到这一点

我对安卓很陌生,自己自学,所以如果人们能就我做错了什么给我建议,或者给我一个应该做什么的例子,我会很感激


感谢所有的回复。

我认为问题在于你在最后一次的
else
中缺少了卷发,这是逻辑所在。就像现在一样,只有
convertToString()
是最后一个
else
语句的一部分,无论设置什么错误,最后四个语句都将执行

试试这个:

cal.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v) 
    {
        boolean failFlag = false;
        // TODO Auto-generated method stub  
        if( eLoan.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eLoan.setError( "A value is required" );
        }
        if( eWage.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eWage.setError( "A value is required" );
        }
        if( eGrant.getText().toString().trim().length() == 0 )
        {
            failFlag = true;
            eGrant.setError( "A value is required" );
        }
        if( eOther.getText().toString().trim().length() == 0 )
        {
            failFlag = true;                
            eOther.setError( "A value is required" );
        }
        // if all are fine
        if (failFlag == false) {
            convertToString();
            converToDouble();               
            inTotal = inLoan + inWage + inGrant + inOther;
            DecimalFormat currency = new DecimalFormat(".00");
            TotalInNum.setText("£" + currency.format(inTotal));
        }
    }

});

如果存在多个错误,此代码将设置多个错误。您的回答只会发出第一次发现的错误的信号。

延迟回答,但可能会帮助需要帮助的人

最简单的方法-->

创建方法如下

public Boolean validateUserInput()
    {
        Boolean isAnyFieldsEmpty=false;

        if (position.getText().toString()==null || position.getText().toString().equalsIgnoreCase(""))
        {
            position.setError("Empty postion");
            isAnyFieldsEmpty=true;
        }
        else{
            position.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (eligiblity.getText().toString()==null || eligiblity.getText().toString().equalsIgnoreCase(""))
        {
            eligiblity.setError("Empty postion");
            isAnyFieldsEmpty=true;
        }
        else{
            eligiblity.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (skillsRequired.getText().toString()==null || skillsRequired.getText().toString().equalsIgnoreCase(""))
        {
            skillsRequired.setError("Empty postion");
            isAnyFieldsEmpty=true;
        }
        else{
            skillsRequired.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (desc.getText().toString()==null || desc.getText().toString().equalsIgnoreCase(""))
        {
            desc.setError("Empty postion");
            isAnyFieldsEmpty=true;
        }
        else{
            desc.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (interviewFrmDate.getText().toString()==null || interviewFrmDate.getText().toString().equalsIgnoreCase(""))
        {
            interviewFrmDate.setError("choose date");
            isAnyFieldsEmpty=true;
        }else{
            interviewFrmDate.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (interviewToDate.getText().toString()==null || interviewToDate.getText().toString().equalsIgnoreCase(""))
        {
            interviewToDate.setError("choose date");
            isAnyFieldsEmpty=true;
        }else{
            interviewToDate.setError(null);
            isAnyFieldsEmpty=false;
        }

        if (interviewToTime.getText().toString()==null || interviewToTime.getText().toString().equalsIgnoreCase(""))
        {
            interviewToTime.setError("choose date");
            isAnyFieldsEmpty=true;
        }else{
            interviewToTime.setError(null);
            isAnyFieldsEmpty=false;
        }

        return isAnyFieldsEmpty;
    }
现在点击你的按钮 按如下方式调用该方法并验证

@overide
public void onclick
{
Boolean isinputEmpty=validateUserInput()
if(isinputEmpty==false)
{

///process your save or whatever processing it is
}

}

只是为了在别人所说的基础上再接再厉。你可以这样做

创建一个验证方法,在EditText中循环,检查它们是否为空,如果为true,则设置错误,然后返回true或false

public boolean validateEditText(int[] ids)
{
    boolean isEmpty = false;

    for(int id: ids)
    {
        EditText et = (EditText)findViewById(id);

        if(TextUtils.isEmpty(et.getText().toString()))
        {
            et.setError("Must enter Value");
            isEmpty = true;
        }
    }

    return isEmpty;
}
列出您的EditText id

int[] ids = new int[]
{
    R.id.section1_item1_textfield,
    R.id.section1_item2_textfield,
    R.id.section1_item3_textfield
};
现在使用验证方法检查是否为空

if(!validateEditText(ids)) 
{
    //if not empty do something
}else{
    //if empty do somethingelse
}
要使用上述方法,您需要

import android.text.TextUtils;

这样做的好处是,你可以简单地把你所有的编辑文本都放到列表中,剩下的就交给你了。维护大量的if语句可能会很烦人且耗时。

我认为解决此问题的最佳方法是以下示例:

private boolean verifyIfEditTextIsFilled(EditText... editText) {

    boolean result = true;

    for (EditText text : editText) {

        if (text.getText().toString().isEmpty()) {

            final View focusView = text;
            text.setError(getString(R.string.error_required));
            focusView.requestFocus();
            result = false;
        }
    }
    return result;
}

这很好用。我遇到了不止一个错误的问题,但是当我把所有的if语句放在第一个if语句中时,我可以把它们按顺序排列,但不能自己把第二个/3个/4个EditText放进去。万分感谢!我有一个额外的疑问,我有四个编辑文本,当任何一个都有价值时,它会继续前进。如果四个有空错误发生,它是如何检查的