Android Clearing all EditText Fields with Clear Bu

2019-01-12 20:45发布

How do I clear all the EditText fields in a layout with a Clear Button. I have a registration Activity that has about 10 different EditTexts. I know I could go and grab a reference to each specifically and then set.Text(""); But I am looking for a more dynamic elegant way. Possibly grab the Layout and loop through all the items in there looking for EditText types and then setting those to "". Not sure how to do that though and tried searching on the web for it but no luck. Any sample code?

10条回答
祖国的老花朵
2楼-- · 2019-01-12 20:56

You can iterate through all children in a view group and clear all the EditText fields:

ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
    View view = group.getChildAt(i);
    if (view instanceof EditText) {
        ((EditText)view).setText("");
    }
}
查看更多
孤傲高冷的网名
3楼-- · 2019-01-12 20:56

You can always do this...it works for me:

mClearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mEditName.getText().clear();
            mEditSummary.getText().clear();
            mEditPrice.getText().clear();
            mEditQuantitiy.getText().clear();

        }
    });

this way you have one fat button that clears all the fields for you once

查看更多
ら.Afraid
4楼-- · 2019-01-12 20:57

Use editText.getText().clear();

查看更多
姐就是有狂的资本
5楼-- · 2019-01-12 21:04

The answer by @Pixie is great but I would like to make it much better.

This method works fine only if all the EditText are in a single(one) layout but when there are bunch of nested layouts this code doesn't deal with them.

After scratching my head a while I've made following solution:

private void clearForm(ViewGroup group) {       
    for (int i = 0, count = group.getChildCount(); i < count; ++i) {
        View view = group.getChildAt(i);
        if (view instanceof EditText) {
            ((EditText)view).setText("");
        }

        if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
            clearForm((ViewGroup)view);
    }
}

To use this method just call this in following fashion:

clearForm((ViewGroup) findViewById(R.id.sign_up));

Where you can replace your R.id.sign_up with the id of root layout of your XML file.

I hope this would help many people as like me.

:)

查看更多
贪生不怕死
6楼-- · 2019-01-12 21:06
   // Update answer

private void clearEditTextGroup(ViewGroup group){


    for(int i=0 ; i< group.getChildCount(); i++){
    View view = group.getChildAt(i);
    if(view instanceof EditText){

    // use one of clear code
    }

     if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
            clearEditTextGroup((ViewGroup)view);

    }


   }

use one of this code to clear your edittext

edittext.getText().clear();

or

edittext.setText(null);

or

edittext.setText("");
查看更多
放我归山
7楼-- · 2019-01-12 21:07

use editText.getText().clear();

or setText as Empty using this below code editText.setText(");

查看更多
登录 后发表回答