Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set the contents of a Edit text from a Button click?

Tags:

android

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

like image 752
Primal Pappachan Avatar asked Apr 23 '10 15:04

Primal Pappachan


1 Answers

To set the contents of an EditText:

EditText text = (EditText) findViewById(R.id.your_text);
text.setText("Display this text");

Instead of trying to pull the text off of your buttons and use it as an integer, I would just add a click listener to each one that "knows" the value of its button:

Button button = (Button) findViewById(R.id.num_1);
button.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        // Do something with the value of the button
    }
});
like image 62
Erich Douglass Avatar answered Nov 14 '22 21:11

Erich Douglass