So I am working with a simple android calculator, I have made 5 buttons, , -, /, * and C. C is supposed to reset all the text field so that they get erased. Basically turned null. Here is the java code, but I do not know how to write the code for the null button. Erasing the content that is inside them the text fields (editTextNumber1, editTextNumber2 and editTextnumber3) These are textboxes, 1 and 2 is where you input the three numbers, 3 is the textbox outputting the answer after the calculation have been made of the numbers that originally was putted in textboxes 1 and 2. like you do on a real calculator when you tap C (Clear button).
package com.example.raknaretest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void AddBtn(View v){
EditText et1 =(EditText)findViewById(R.id.editTextNumber1);
EditText et2 =(EditText)findViewById(R.id.editTextNumber2);
EditText et3 =(EditText)findViewById(R.id.editTextNumber3);
int n1 = Integer.parseInt(et1.getText().toString());
int n2 = Integer.parseInt(et2.getText().toString());
int result = n1 n2;
et3.setText("= " result);
}
public void SubBtn(View v){
EditText et1 =(EditText)findViewById(R.id.editTextNumber1);
EditText et2 =(EditText)findViewById(R.id.editTextNumber2);
EditText et3 =(EditText)findViewById(R.id.editTextNumber3);
int n1 = Integer.parseInt(et1.getText().toString());
int n2 = Integer.parseInt(et2.getText().toString());
int result = n1 - n2;
et3.setText("= " result);
}
public void MultBtn(View v){
EditText et1 =(EditText)findViewById(R.id.editTextNumber1);
EditText et2 =(EditText)findViewById(R.id.editTextNumber2);
EditText et3 =(EditText)findViewById(R.id.editTextNumber3);
int n1 = Integer.parseInt(et1.getText().toString());
int n2 = Integer.parseInt(et2.getText().toString());
int result = n1 * n2;
et3.setText("= " result);
}
public void DivBtn(View v){
EditText et1 =(EditText)findViewById(R.id.editTextNumber1);
EditText et2 =(EditText)findViewById(R.id.editTextNumber2);
EditText et3 =(EditText)findViewById(R.id.editTextNumber3);
int n1 = Integer.parseInt(et1.getText().toString());
int n2 = Integer.parseInt(et2.getText().toString());
int result = n1 / n2;
et3.setText("= " result);
}
// public void ClearBtn(View view) {
// }
}
CodePudding user response:
To clear the text fields use
et.getText().clear();
or
et.setText("")
CodePudding user response:
You need to give click listner for particular education for clear data if you want to clear your edit text after completion you should try this
this code in onCreate()
editText.setOnClickListener(this);
after this you should code like this in new function
@Override
public void onClick(View v) {
editText.getText().clear(); //or you can use editText.setText("");
}
Hope it helps try to clear next time in asking questions you can get down votes easily.
CodePudding user response:
See other answers on how to clear the text.
I suggest you save your EditText
s in local variables and create methods for fetching their values. Also, method names should start with a lower case character and be private
(since you probably don't want to use them outside of MainActivity
) and variable names should be significant:
package com.example.raknaretest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et1, et2, et3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editTextNumber1);
et2 = (EditText) findViewById(R.id.editTextNumber2);
et3 = (EditText) findViewById(R.id.editTextNumber3);
}
private void addBtn(View v) {
int result = value(et1) value(et2);
displayResult(result);
}
private void subBtn(View v) {
int result = value(et1) - value(et2);
displayResult(result);
}
private void multBtn(View v) {
int result = value(et1) * value(et2);
displayResult(result);
}
private void divBtn(View v) {
int result = value(et1) / value(et2);
displayResult(result);
}
private int value(EditText editText) {
return Integer.parseInt(editText.getText().toString());
}
private void displayResult(int result) {
et1.getText().clear();
et2.getText().clear();
et3.setText("= " result);
}
}