3

これに一晩中立ち往生しており、簡単な解決策はないようです。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をチェックしようとしましたが、うまくいきませんでした. これを行うためのより簡単な方法があると確信しています。

私はAndroidにまったく慣れていないので、自分で教えているので、私が間違っていることについて人々が私にアドバイスしてくれたり、私がすべきことの例を教えてくれたりしてくれれば幸いです。

回答者全員に感謝します。

4

4 に答える 4

4

else問題は、ロジックが置かれている最後の にカーリーがないことだと思います。現時点では、最後の 4 つのステートメントの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));
        }
    }

});

複数のエラーが存在する場合、このコードは複数のエラーを設定します。Yours は、最初に見つかったエラーのみを通知します。

于 2013-06-12T22:12:11.020 に答える
4

他の人が言ったことに基づいて構築するだけです。あなたはこのようなことをすることができます...

EditTexts をループし、それらが空かどうかを確認し、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;

このようにすることの良い点は、すべての EditTexts をリストに入れるだけで、あとは自動でやってくれることです。if ステートメントの膨大なチャンクを維持することは、面倒で時間がかかる場合があります。

于 2017-02-14T18:29:04.733 に答える
0

遅い回答ですが、困っている人を助けるかもしれません。

最も簡単な方法 -->

以下のようにメソッドを作成します

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
}

}
于 2016-11-15T07:30:38.930 に答える