15

I have an app which at current will on the press of a button eg. the number 1 button change a text view to have the number 1 in it. What i need to do is append the view so that say when the number 3 is pressed the text view will say 13 instea of just 3. Here is the switch statement i'm using to handle the button presses.

@Override
public void onClick(View c) {

    switch (c.getId()) 
    {
        case R.id.keypad_1:
            TextView prs1 = (TextView) findViewById(R.id.diff);
            prs1.setText("1");

    }

}
nexus490
  • 797
  • 5
  • 11
  • 21

3 Answers3

45

Use the TextView.append() method.

Antonio
  • 19,451
  • 13
  • 99
  • 197
Maz
  • 764
  • 5
  • 5
3

You can use append() method of textView and the difference between setText and append can be found here. If you make the following changes I think the code would work as expected.

TextView prs1 = (TextView) findViewById(R.id.diff);
public void onClick(View c){
    switch (c.getId()) 
    {
        case R.id.keypad_1:
            prs1.append("1");
    }
}
Community
  • 1
  • 1
0

Possibly try doing this:

prs1.setText(prs1.getText() + "1");

Edit: Sorry I just realized you wanted it to say "13"

So you would want to do this:

String text = prs1.getText();
prs1.setText("1" + text);

"when the screen first comes up there is a ? in the text view, then when i press the 1 button i want the text view to be 1 then when i press 3 it would need to be 13.":

Do this then:

String text = prs1.getText();
if(text.contains("?"))
            text = text.replace("?", "");
    prs1.setText("1" + text);