Reputation: 802
I am trying to create a simple number Pad. I am getting the information to the TextView just fine BUT each time I click on a new Button it removes the old one. How do I keep what was in the TextView and add what is new. You push 1 it goes to TextView, You push two it goes to TextView but removes the "1" that was in there before. I want "1" to stay so when you push both button you have 12.
final TextView singlevalue = (TextView) findViewById(R.id.priceInput);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Button one = (Button) findViewById(R.id.one);
final String stringone = one.getText().toString();
singlevalue.setText(String.valueOf(stringone);
}
});
I know something like this +"" goes after (stringone) but what???
two.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Button two = (Button) findViewById(R.id.two);
final String stringtwo = two.getText().toString();
singlevalue.setText(String.valueOf(stringtwo)+"");
}
});
Upvotes: 0
Views: 819
Reputation: 39538
I would like to blring some improvements in addition to Ram Kiran answer.
First of all, I don't think that findViewById should go in the onClick method. Your app will try to find the view every time you press on a button, and I am pretty sure this can be done once. A first improvement should be to remove that findViewById of the onClick method.
As a second improvement, you could benefit from the onClick method in your xml file and use android:onclick="addText"
This will just let you create a method:
public void addText(View v)
in your Java code once instead of doing it for each TextView. The View v in argument contains the data as text, so no need to even make a findViewById !!! ((TextView) v).getText() is enough to get the text value!
So, your code will be a few lines instead of a huge java file
As a third improvement, I would use a button (or ImageButton) instead of a TextView and use the multiple state (Button will have that already done for you) to use a visual clue when button is pressed or selected.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/login_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="@drawable/login" /> <!-- default -->
</selector>
If you need more info, just drop a comment ;-)
Upvotes: 1
Reputation: 525
Try this:
singlevalue.setText(singlevalue.getText().toString() + "" + String.valueOf(stringtwo));
Hope help
Upvotes: 1
Reputation: 5246
This should work...
final TextView singlevalue = (TextView) findViewById(R.id.priceInput);
final Button one = (Button) findViewById(R.id.one);
final Button two = (Button) findViewById(R.id.two);
String stringone = new String();
String stringtwo = new String();
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
stringone = one.getText().toString();
singlevalue.setText(stringone+""+stringtwo);
}
});
two.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
stringtwo = two.getText().toString();
singlevalue.setText(stringone+""+stringtwo);
}
});
Upvotes: 1
Reputation: 21181
you need to take one variable globally and add the new entered value when button clicked
ex say
public String totalString = "";
final TextView singlevalue = (TextView) findViewById(R.id.priceInput);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Button one = (Button) findViewById(R.id.one);
final String stringone = one.getText().toString();
totalString = totalString+""+stringone ;
singlevalue.setText(totalString);
}
});
two.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final Button two = (Button) findViewById(R.id.two);
final String stringtwo = two.getText().toString();
totalString = totalString+""+stringtwo ;
singlevalue.setText(totalString);
}
});
Upvotes: 1