Android开发中,重置按钮是指一个Button或ImageButton控件,其功能是将表单或输入框中的内容清空,供用户重新输入。在Android中,我们可以使用以下几种方式实现重置按钮功能:
1. 代码实现
可以在按钮的点击事件中,将所有需要清空的表单元素设为空或默认值。例如:
```
Button resetButton = (Button) findViewById(R.id.btn_reset);
resetButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
editText1.setText("");
editText2.setText("");
checkBox1.setChecked(false);
checkBox2.setChecked(false);
radioButton1.setChecked(false);
radioButton2.setChecked(false);
}
});
```
2. XML实现
可以在XML文件中定义一个重置按钮,以及需要清空的表单元素和其默认值。例如:
```