I have a simple calculator that has six EditText views where users will enter numbers to perform a simple math function. This app will satisfy a repetitive task wherein the user
The most basic way to do this would be to simply reset your EditText
views. If you have logic that drives the update of these fields, then resetting them to an empty String
and requesting a "recalculation" to update the hints.
Something like this:
private void onClear()
{
EditText firstField = (EditText)this.findById(R.id.firstField);
EditText secondField = (EditText)this.findById(R.id.secondField);
//...etc...
if (firstField != null) firstField.setText("");
if (secondField != null) secondField.setText("");
updateHints();
}
private void updateHints()
{
//Logic for your "hints"
}
No, you have to implement it yourself. I suggest you create an int array with the IDs of all your EditTexts
you want to reset (R.id.xyz
). Then create a loop to .setText()
to each of the EditTexts from the array, which you can call every time you want the fields to be cleared. Something like:
private void resetFields() {
EditText temp;
for (int i = 0; i < myEditTexts.length; i++) {
temp = (EditText) findViewById(textViews[i]);
temp.setText("");
}
}
My answer is just a better way but it still a kind of hard code. Anyone could find another way faster, please share.
protected void clearForm() {
Button btnClear = (Button) findViewById(R.id.btn_clear_text);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText temp;
final int[] txtId = new int[] {
R.id.txt_s_free_word,
R.id.txt_s_property_name,
R.id.txt_s_ad_expense_from,
R.id.txt_s_ad_expense_to,
R.id.txt_s_station
};
for (int i = 0; i < txtId.length; i++) {
temp = (EditText) findViewById(txtId[i]);
temp.setText(null);
}
}
});
}