Android Clearing all EditText Fields with Clear Button

狂风中的少年 提交于 2019-11-27 17:10:12
necixy

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.

:)

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

Use editText.getText().clear();

after onclick of any action do below step

 ((EditText) findViewById(R.id.yoursXmlId)).setText("");

or Clear all EditText fields by iterating all childrens:

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

or use simple below step :

editText.getText().clear();

Might be helpful.

In my case I've done this.

public static void resetForm(ViewGroup group) {
    for (int i = 0, count = group.getChildCount(); i < count; ++i) {
        View view = group.getChildAt(i);
        if (view instanceof EditText) {
            ((EditText) view).getText().clear();
        }

        if (view instanceof RadioGroup) {
            ((RadioButton)((RadioGroup) view).getChildAt(0)).setChecked(true);
        }

        if (view instanceof Spinner) {
            ((Spinner) view).setSelection(0);
        }

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

I used this for nested LinearLayout to clear EditTexts and RadioButtons

//method clear private void clear(ViewGroup group) { for(int i=0,count=group.getChildCount();i

        if(view instanceof  LinearLayout)
        {
            clear((ViewGroup) view);
        }
         else if(view instanceof EditText)
        {
            ((EditText) view).getText().clear();
        }
        if (view instanceof  RadioButton)
        {
            ((RadioButton) view).setChecked(false);
        }
    }//end for
}//end method clear

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

It's very simple.Type this in your function of button-

finish();
startActivity(this,YourCurrentActivity.class);

As Simple as that. Welcome.

   // 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("");

use editText.getText().clear();

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!