Reset code for EditText and TextView for Android

I am new to programming. I made a simple Android application that asked the user to put 2 numbers, perform calculations, display the answer by clicking the calculation button. Now I'm trying to configure the reset button to clear all fields. A tested solution for, but still can not figure out how to do it. This is my code:

public void calculate(View view) { EditText number1 = (EditText)findViewById(R.id.num1ID); EditText number2 = (EditText)findViewById(R.id.num2ID); Double num1Value = Double.parseDouble(number1.getText().toString()); Double num2Value = Double.parseDouble(number2.getText().toString()); Double resultValue = num1Value - num2Value; TextView resultDisplay = (TextView)findViewById(R.id.resultID); resultDisplay.setText(Double.toString(resultValue)); } 

Thanks.

+6
source share
7 answers

Just add a resetButton button to the layout

 Button resetButton = (Button) findViewById(R.id.btnReset); resetButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { number1.setText(""); number2.setText(""); resultDisplay.setText(""); } }); 
+3
source

There is a method like this

 public void resetTextViews() { number1.setText(""); number2.setText(""); resultDisplay.setText(""); } 

Then simply install the click listener on your button, which calls this method.

+3
source

There is no method to clear data in EditText. If you want to clear the data you must install

 .setText(""); 

There is no reset field in the android documentation itself.

+2
source

Do you want to reset your edittext?

 private void resetNumbersFields() { number1.setText(""); number2.setText(""); resultDisplay.setText(""); // if you want you can add setHint to add hint to your EditText when your field is empty } 

I hope that helps you

+2
source

The exact code for your program.

 public void calculate(View view) { EditText number1 = (EditText)findViewById(R.id.num1ID); EditText number2 = (EditText)findViewById(R.id.num2ID); Button B_reset=(Button)findViewById(R.id.bReset); // create a button on your Layout for Reset as "bReset" Double num1Value = Double.parseDouble(number1.getText().toString()); Double num2Value = Double.parseDouble(number2.getText().toString()); Double resultValue = num1Value - num2Value; TextView resultDisplay = (TextView)findViewById(R.id.resultID); resultDisplay.setText(Double.toString(resultValue)); //reset code B_reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { number1.setText(""); // reset the EditText number1 number2.setText(""); // reset the EditText number2 resultDisplay.setText(""); // reset the Textview resultDisplay } }); } 
+2
source

edittext1.setText(StringUtility.EMPTY); . This will help.

+2
source

The OnClick Reset button sets setText from EditText to empty lines. You can follow the link below, it is useful for beginners http://www.mkyong.com/tutorials/android-tutorial/

+1
source

All Articles