开发者

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

开发者 https://www.devze.com 2022-12-28 11:50 出处:网络
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 w

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 n开发者_C百科umber 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


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


Try the following code.

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

Don't use x.setText(); instead, use x.append(Your variable or value here). This will not clear the text that came from the previous button.


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 getText just 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:

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

...which will of course throw an exception if the text of myButton does 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 and getText() if you just want to append something to the current value:

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


To Set the text of button in 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 simple calcultaor see here it will help you to create a calculator in android.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号