I am a rookie to android. I am thinking of implementing a simple calculator in android to get a hold of the basics in android. I want to display a keypad with numbers and mathematical operations and when the user presses the keys the corresponding number is displayed in edit text. I tried using gettext() and updating the contents of edit text but it shows just the contents of pressed button. Also how do I read the contents of button so as to do mathematical operations in code? Any help would be much appreciated.
regards,
Primal
Treating the caption of the button as the actual data is something that is pretty much only acceptable practice when you’re doing the digit-buttons in a calculator, so I don’t know if that holds for ‘android basics’ 😉
Regardless;
You’re stating you want to display the corresponding number, when the user presses a key (button?). And then you say
getTextjust shows you the content of the pressed button… Is that not exactly what you’re asking for? You might need to provide a bit of code and show us what’s not working the way you intended. But if a button has the text ‘8’, and you want to treat that as an eight in mathematical operations, you need to parse it:…which will of course throw an exception if the text of
myButtondoes not resolve to an integer.EDIT, AS OF COMMENT
From the explanation of your problem you gave in your comment, yes,
setText()resets the text entirely to the value it’s being passed, but you can use a combination of that andgetText()if you just want to append something to the current value: