如何在Android中清除EditText?

时间:2017-10-19 10:52:49

标签: android

我正在开发一个Android应用程序。在这里,我需要在一些EditText中设置值。然后我必须清除所有。我怎样才能做到这一点? EditData.java代码如下:

    morning = (EditText) findViewById(R.id.morning);
    noon = (EditText) findViewById(R.id.noon);
    afternoon = (EditText) findViewById(R.id.afternoon);
    night = (EditText) findViewById(R.id.night);
    before_meal = (EditText) findViewById(R.id.before_meal);
    after_meal = (EditText) findViewById(R.id.after_meal);
    hourly = (EditText) findViewById(R.id.hourly);
    days = (EditText) findViewById(R.id.days);

    morning.setText(String.valueOf(scheduleTime.charAt(0)));

    noon.setText(String.valueOf(scheduleTime.charAt(1)));

    afternoon.setText(String.valueOf(scheduleTime.charAt(2)));

    night.setText(String.valueOf(scheduleTime.charAt(3)));

    before_meal.setText(String.valueOf(scheduleTime.charAt(4)));

    after_meal.setText(String.valueOf(scheduleTime.charAt(5)));

    hourly.setText(String.valueOf(scheduleTime.charAt(6)));

    days.setText(String.valueOf(scheduleTime.charAt(7)));

2 个答案:

答案 0 :(得分:2)

添加此代码以清除所有editText:

    morning.setText("");

    noon.setText("");

    afternoon.setText("");

    night.setText("");

    before_meal.setText("");

    after_meal.setText("");

    hourly.setText("");

    days.setText("");

答案 1 :(得分:1)

你可以通过以下方式做到

1)myEditText.setText(“”);

2)其中clear被注册为布局文件中按钮的onclick处理程序,如此

<ImageButton android:id="@+id/ClearButton"
    android:text="@string/ClearButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clear"           <<<--------here
    android:src="@drawable/clear"
/>