Reputation: 2253
I am trying to make a calculator app that displays everything the user clicks on the edit text screen, however, when the user clicks a button, the edit text screen still remains blank. I have put some print statements to see what happens and this is what I have found out:
The print statement in updateEditText()
is always blank, even if the user clicks a button, yet the print statement in getValuesPressed()
successfully prints out where buttons were clicked.
I do not understand what I'm doing wrong.
Here is what I have:
void updateEditText(){
EditText et = (EditText) findViewById(R.id.editText);
System.out.println("THE VALUE PRESSED IS: " + getValuesPressed());
et.setText(getValuesPressed());//problem here
}
//return and concat the buttons that were clicked
String getValuesPressed(){
for(int i = 0; i < buttonList.length; i++){
final int j = i;
buttonList[i].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("THE BUTTON WAS PRESSED: " + buttonList[j].getText());
switch(j){
case 0://CLEAR
textOnScreen = "0";
break;
case 5://SQRT
solveEquation();
//CASE: IF IT IS NEG U CANT TAKE THE SQRT!!!!
textOnScreen = String.valueOf(Math.sqrt(Double.parseDouble(textOnScreen)));
break;
case 18: //NEGATIVE
if(textOnScreen.charAt(textOnScreen.length() - 1) != '-')
textOnScreen = textOnScreen + "-";
else
textOnScreen = textOnScreen.substring(0, textOnScreen.length() - 1);
break;
case 19: //BACKSPACE
if(textOnScreen.length() == 1)
textOnScreen = "0";
else
textOnScreen = textOnScreen.substring(0, textOnScreen.length() - 1);
break;
case 20: //SOLVE
solveEquation();
break;
default://concat all of the other buttons pressed
textOnScreen = textOnScreen + buttonList[j].getText();
break;
}
}
});
}
return textOnScreen;
}
Upvotes: 1
Views: 1142
Reputation: 237
Your whole code doesn't really make sense to me. The first thing you should do is to add an onClickListener
to all your buttons. This listener could look something like this:
@Override
public void onClick(View v) {
System.out.println("THE BUTTON WAS PRESSED: " + ((Button)v).getText();
switch(v.getId()){
case R.id.<ID of this Button>://CLEAR
et.setText("0");
break;
case R.id.<ID of this Button>://SQRT
solveEquation();
//CASE: IF IT IS NEG U CANT TAKE THE SQRT!!!!
et.setText(String.valueOf(Math.sqrt(Double.parseDouble(textOnScreen))));
break;
case R.id.<ID of this Button>: //NEGATIVE
if(et.getText().charAt(et.getText().length() - 1) != '-')
et.setText(et.getText + "-");
else
et.setText(et.getText().toString().substring(0, et.getText().length() - 1));
break;
case R.id.<ID of this Button>: //BACKSPACE
if(et.getText().length() == 1)
et.setText("0");
else
et.setText(et.getText().toString()substring(0, et.getText().length() - 1));
break;
case R.id.<ID of this Button>: //SOLVE
solveEquation();
break;
}
}
Upvotes: 1
Reputation: 370
You are not supposed to set the OnClickListener more than once for each button. Change the String getValuesPressed() function to void setButtonClickEvents().
Also, you don't need the updateEditText() function. Instead of concatenating the textOnScreen, concatenate the EditText directly:
case 0://CLEAR
et.setText("0");
break;
case 5://SQRT
solveEquation();
//CASE: IF IT IS NEG U CANT TAKE THE SQRT!!!!
et.setText(String.valueOf(Math.sqrt(Double.parseDouble(et.getText().toString())));
break;
and so on.
Upvotes: 2