简体   繁体   English

清除 EditText 字段按钮中的所有文本

[英]Clear all text in EditText fields button

I am trying to run a programme in Android to clear all text in the EditText fields when the user presses the clear all button.我正在尝试在 Android 中运行一个程序,以在用户按下全部清除按钮时清除 EditText 字段中的所有文本。 Below is the code I have so far and at the moment it is not working as planned.下面是我到目前为止的代码,目前它没有按计划工作。 I am new to java and adroid app development.我是java和adroid应用程序开发的新手。 Kindly let me know how I can resolve this problem, with examples,if possible.如果可能,请举例说明如何解决此问题。 Many thanks in advance.提前谢谢了。

CEMMainActivity.java CEMMainActivity.java

public class CEMMainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { // runs every time the app runs
    super.onCreate(savedInstanceState); // loads saved instant state
    setContentView(R.layout.activity_cemmain);//sets what the app will look like based on the graphical design created

    if (savedInstanceState == null) {// action if app does not start up correctly
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    // Clears all text in EditText fields

    Button clearalltext  = (Button) findViewById(R.id.cleartext); 
    clearalltext.setOnClickListener(new OnClickListener() {
        public void onClick(View c) {           
        ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);
        clearText(group);       
        });
    }

        private void clearText((ViewGroup)group); {
            // TODO Auto-generated method stub
            for (int i = 0, count = group.getChildCount(); i < count; ++i) {
        View view = group.getChildAt(i);
        if (view instanceof EditText) {
            ((EditText)view).setText("");

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

fragment_cemmain.xml fragment_cemmain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="clinical.equipment.monitor.CEMMainActivity$PlaceholderFragment" >
.....

<Button
    android:id="@+id/cleartext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/findequip"
    android:layout_marginTop="56dp"
    android:onClick="cleartext"
    android:text="@string/cleartext" />

....

</RelativeLayout>

(ViewGroup) findViewById(R.id.cleartext); i think cleartext is button not a viewgroup you can use getParent() method of onclick argument parameter c like c.getParent() which return ViewGroup Relative Layout then you can fetch all children which wil define by you in clearText Method ...我认为明文是按钮而不是视图组,您可以使用 onclick 参数参数 c 的 getParent() 方法,例如c.getParent()返回 ViewGroup 相对布局,然后您可以获取由您在 clearText 方法中定义的所有子项...

or you can give id to relativelayout also或者你也可以给相对布局提供 id

  // Clears all text in EditText fields

    Button clearalltext  = (Button) findViewById(R.id.cleartext); 
    clearalltext.setOnClickListener(new OnClickListener() {
        public void onClick(View c) {           
        ViewGroup group = (ViewGroup) c.getParent();
        clearText(group);       
        });
    }

I think problem with you are passing button id to view group so its child count will be zero.please pass you parent layout id to view group .我认为您的问题是将按钮 id 传递给查看组,因此其子计数将为零。请将父布局 id 传递给查看组。 make changes to below id更改以下 ID

ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);

replace cleartext with your parent layout id.用您的父布局 ID 替换明文。 ie RelativeLayout id即相对布局 ID

 ViewGroup group = (ViewGroup) findViewById(R.id.cleartext); 

It will access button and will only clear text of the button.它将访问按钮并且只会清除按钮的文本。 Assign an id to RelativeLayout and use it to clear text of all the childs.为 RelativeLayout 分配一个 id 并使用它来清除所有孩子的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM