Android使用清除按钮清除所有EditText字段
如何用Clear Button清除布局中的所有EditText。我有一个注册活动,具有大约10个不同的EditTexts。我知道我可以专门获取每个的引用,然后set.Text(""); 但我正在寻找一种更具活力的优雅方式。 可能会抓住Layout,然后遍历其中的所有项目以寻找EditText类型,然后将其设置为""。虽然不确定如何执行此操作,但尝试在网上搜索它,但是没有运气。 任何示例代码?
10个解决方案
62 votes
@Pixie的答案很好,但我想做得更好。
仅当所有EditText都在一个(一个)布局中时,此方法才能正常工作,但是当存在一堆嵌套布局时,此代码将不处理它们。
挠了一下头后,我提出了以下解决方案:
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);
}
}
要使用此方法,只需按以下方式调用此方法:
clearForm((ViewGroup) findViewById(R.id.sign_up));
您可以在其中用XML文件的根布局ID替换R.id.sign_up。
我希望这会对像我这样的许多人有所帮助。
:)
necixy answered 2020-07-20T01:36:21Z
29 votes
您可以遍历视图组中的所有子级并清除所有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("");
}
}
Michael answered 2020-07-20T01:36:41Z
8 votes
使用editText.getText().clear();
Malder answered 2020-07-20T01:37:01Z
1 votes
在单击任何动作后,请执行以下步骤
((EditText) findViewById(R.id.yoursXmlId)).setText("");
或通过迭代所有子项来清除所有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();
可能会有所帮助。
Rahul Baradia answered 2020-07-20T01:37:34Z
0 votes
就我而言,我已经做到了。
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);
}
}
Kasim Rangwala answered 2020-07-20T01:37:53Z
0 votes
我将此用于嵌套的LinearLayout来清除“编辑文本”和“单选按钮”
//方法清除 私有无效清除(ViewGroup组) { for(int i = 0,count = group.getChildCount(); iif(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
Siamak Eshghi answered 2020-07-20T01:38:18Z
0 votes
您可以随时这样做...对我有用:
mClearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditName.getText().clear();
mEditSummary.getText().clear();
mEditPrice.getText().clear();
mEditQuantitiy.getText().clear();
}
});
这样,您可以使用一个胖按钮一次为您清除所有字段
RileyManda answered 2020-07-20T01:38:42Z
0 votes
非常简单。在您的按钮功能中输入以下内容:
finish();
startActivity(this,YourCurrentActivity.class);
就如此容易。欢迎。
ykb answered 2020-07-20T01:39:06Z
-1 votes
// 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);
}
}
使用以下代码之一清除您的edittext
edittext.getText().clear();
要么
edittext.setText(null);
or
edittext.setText("");
Mina Fawzy answered 2020-07-20T01:40:30Z
-1 votes
用editText.setText(");
或使用以下代码将setText设置为EmptyeditText.setText(");
Mohamed answered 2020-07-20T01:40:54Z