[英]Android Clearing all EditText Fields with Clear Button
How do I clear all the EditText
fields in a layout with a Clear Button
.如何使用
Clear Button
布局中的所有EditText
字段。 I have a registration Activity that has about 10 different EditTexts
.我有一个注册 Activity 大约有 10 个不同的
EditTexts
。 I know I could go and grab a reference to each specifically and then set.Text("")
;我知道我可以去获取对每个的引用,然后
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 ""
.可能会抓取
Layout
并遍历其中的所有项目以查找EditText
类型,然后将它们设置为""
。 Not sure how to do that though and tried searching on the web for it but no luck.不知道如何做到这一点,并尝试在网上搜索它,但没有运气。 Any sample code?
任何示例代码?
The answer by @Pixie is great but I would like to make it much better. @Pixie 的回答很棒,但我想让它变得更好。
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.仅当所有 EditText 都在单个(一个)布局中时,此方法才能正常工作,但是当有一堆嵌套布局时,此代码不会处理它们。
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.您可以用 XML 文件的根布局的 id 替换 R.id.sign_up 的位置。
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:您可以遍历视图组中的所有子项并清除所有
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("");
}
}
使用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:或通过迭代所有子项清除所有 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("");
}
}
or use simple below step :或使用以下简单的步骤:
editText.getText().clear();
Might be helpful.可能会有所帮助。
It's very simple.Type this in your function of button-很简单。在你的按钮功能中输入这个 -
finish();
startActivity(this,YourCurrentActivity.class);
As Simple as that.就如此容易。 Welcome.
欢迎。
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我将它用于嵌套的 LinearLayout 以清除 EditTexts 和 RadioButtons
//method clear private void clear(ViewGroup group) { for(int i=0,count=group.getChildCount();i //方法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这样你就有了一个可以为你清除所有字段的胖按钮
I created a reset button and write a reset onclick method as follow:我创建了一个重置按钮并编写了一个重置 onclick 方法,如下所示:
public void reset(View View){
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
It works for me and also it works without addflags part.它对我有用,也可以在没有 addflags 部分的情况下工作。 But I would like to know whether it is a good approach or not.
但我想知道这是否是一个好方法。
// 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();
使用
editText.getText().clear();
or setText as Empty using this below code editText.setText(");
或 setText 为 Empty 使用以下代码
editText.setText(");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.