Android: How to set content Edit text using a button?

I am new to android. I am thinking of implementing a simple calculator in android in order to master the basics of Android. I want to display a keyboard with numbers and mathematical operations, and when the user presses the keys, the corresponding number is displayed in a text editor. I tried using gettext () and updating the contents of the edit text, but it only shows the contents of the pressed button. Also, how do I read the contents of a button to perform math operations in code? Any help would be greatly appreciated.

Yours faithfully,

Original

+5
source share
4 answers

, - -, , , , " Android"; )

;

, , (?). , getText ... , ? , , , . "8", , :

Integer myNumber = Integer.parse(myButton.getText());

... , , , myButton .

,

, , , setText() , getText(), - :

myEditText.setText( myEditText.getText() + " " + myButton.getText() );
+5

EditText:

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

, , , "" :

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
    }
});
+6

.

EditText x = (EditText)findviewbyid(R.id.abc);

x.setText(); x.append(Your variable or value here). , .

+4

EditText:

EditText edittext=(EditText)findviewById(R.id.edittext); 
Button num1=(Button)findViewById(R.id.button_num1);
Button num2=(Button)findviewById(R.id.button_num2);
num1.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) {
        edittext.append("1");
    } 
});
num1.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View v) {
        edittext.append("2");
    } 
});

for a simple calculator, see here this will help you create a calculator in Android.

+2
source

All Articles