web-dev-qa-db-fra.com

Android Effacement de tous les champs EditText avec le bouton Clear

Comment effacer tous les champs EditText d'une présentation avec un Clear Button. J'ai une activité d'inscription qui comporte environ 10 variables EditTexts. Je sais que je pourrais aller chercher une référence à chacune d’elles, puis à set.Text(""); Mais je cherche une manière élégante plus dynamique. Vous pouvez éventuellement récupérer la variable Layout et parcourir tous les éléments à la recherche de types EditText, puis les définir à "" Je ne sais pas comment faire cela et j'ai essayé de chercher sur le Web, mais pas de chance. Un exemple de code?

43
JPM

La réponse de @Pixie est excellente, mais j'aimerais l'améliorer beaucoup.

Cette méthode ne fonctionne correctement que si tous les EditText sont dans une seule et même disposition, mais lorsqu'il existe plusieurs dispositions imbriquées, ce code ne les traite pas.

Après me gratter la tête un moment, j'ai fait la solution suivante:

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

Pour utiliser cette méthode, appelez simplement ceci de la manière suivante:

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

Où vous pouvez remplacer votre R.id.sign_up par l'id de la disposition racine de votre fichier XML.

J'espère que cela aiderait beaucoup de gens comme moi.

:)

59
necixy

Vous pouvez parcourir tous les enfants d'un groupe de vues et effacer tous les champs EditText:

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

Utilisez editText.getText().clear();

9
Malder

après avoir cliqué sur une action, faites en-dessous de l'étape

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

ou Effacez tous les champs EditText en itérant tous les enfants:

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

ou utilisez simple étape ci-dessous:

editText.getText().clear();

Pourrait être utile.

1
Rahul Baradia

Dans mon cas, je l'ai fait.

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);
    }
}
0
Kasim Rangwala

J'ai utilisé cela pour LinearLayout imbriqué pour effacer EditTexts et RadioButtons

// méthode clear private void clear (groupe ViewGroup) { pour (int i = 0, nombre = 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
0
Siamak Eshghi

C'est très simple. Tapez ceci dans votre fonction de bouton-

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

Aussi simple que cela ... Bienvenue.

0
ykb

Vous pouvez toujours faire ça ... ça marche pour moi:

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

        }
    });

de cette façon, vous avez un gros bouton qui efface tous les champs pour vous une fois

0
RileyManda