How to Check whether a value is entered in editext

2019-08-31 22:50发布

问题:

I have around 5 edittexts.When i click submit button,it has to check whether all fields are entered or not.Along with it if any value is not entered,the focus has to go to that desired edittext.If there are many editexts empty,then the focus has to go in top down order.I have used seterror method but when i type in that editext the error msg is not going.After entering values the focus is not going to the next edittext.how to solve this issue?

caseno,dateloss,policy_rep,reg_book,Dri_lic are the different editexts used.I have written code for one editext below

caseno.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasfocus)

                {



                     if(!hasfocus && caseno.getText().length()==0)
                        {    
                              new Handler().postDelayed(new Runnable()
                              {

                              @Override
                              public void run() {
                                   //ev2.clearFocus();



                                dateloss.clearFocus();
                                policy_rep.clearFocus();
                                reg_book.clearFocus();
                                Dri_lic.clearFocus();
                                  caseno.requestFocus();
                                  caseno.setError("Enter this field");

                              }
                        }, 100);

                     }





                }
            });



btnsubmit.setOnClickListener(new OnClickListener() {



        @Override
        public void onClick(View v) {





            if(caseno!=null||dateloss!=null||policy_rep!=null||reg_book!=null||Dri_lic!=null)
            {


            Send_reportclaim_Async reportsync=new Send_reportclaim_Async();
            reportsync.execute();
            }

        }
    });

回答1:

Android provides a setError() method for this:

if(edttxtDescription.getText().toString().trim().equals(""))
{
    edttxtDescription.setError("Please provide description");
}

Define a method to check whether your EditTexts have valid data:

private boolean validateEditTexts()
{
    boolean valid = true;

    if(edttxtDescription.getText().toString().trim().equals(""))
    {
        edttxtDescription.setError("Please provide description");
        valid = false;
    }

    // Similarly check all your EditTexts here and set the value of valid

    ......
    ......

    return valid;

}

To validate all your EditTexts, call validateEditTexts() which will return true or false accordingly.

btnsubmit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        if(validateEditTexts()){

            Send_reportclaim_Async reportsync=new Send_reportclaim_Async();
            reportsync.execute();

        }
    }
});

Try this. This will work.



回答2:

Check this:

for(EditText edit : editTextList){
    if(TextUtils.isEmpty(edit.getText()){
        // EditText is empty

    }
}


回答3:

Maintain array of EditText references: Like

EditText[] allEts = { caseno, dateloss, policy_rep, reg_book, Dri_lic };

Write the below code in onClick of submit button:

    for (EditText editText : allEts) {
         String text = editText.getText().toString();
         if (text.length() == 0) {
            editText.setError("enter this field");
            editText.requestFocus();
            break;
        }
    }

And, implement addTextChangedListener for all edittexts to clear the error after entering the text.

    caseno.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Editable text = caseno.getText();
            if (caseno.getError() != null && text != null
                    && text.length() > 0) {
                caseno.setError(null);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


回答4:

Use this on your button click

if(!textView1.toString().isEmpty() && !textView2.toString().isEmpty() && ...)
{
 ............
}


回答5:

1) create this method

public static boolean isNullOrEmpty(String input) {
    return input == null || input.isEmpty();
}

2) send data to it for validation

  boolean answer = isNullOrEmpty(editText.gettext().toString());


回答6:

    btnsubmit.setOnClickListener(new OnClickListener() {





        @Override
        public void onClick(View v) {




 if(TextUtils.isEmpty(caseno.getText().toString()))
             {


         caseno.requestFocus();
         Toast.makeText(getApplicationContext(),"Enter the required fields", Toast.LENGTH_LONG).show();

            return;
         }

            if(TextUtils.isEmpty(dateloss.getText().toString()))
             {


                dateloss.requestFocus();
                Toast.makeText(getApplicationContext(),"Enter the required fields", Toast.LENGTH_LONG).show();

            return;
         }

}

Currently this setup is working for me...Thnks those who answered