Save a Textview value and add another

Asked

Viewed 73 times

1

I’m Creating a keyboard to add value to a Textview. I was able to add a value only to Textview. But what I wanted to do was keep the current value and add another to the side so that the user can make a number to add to Textview.

At the Onclick event I did it:

 switch (view.getId()) {
            case R.id.buttonNum1:
                txtNum.setText("1");
                break;
            case R.id.buttonNum2:
                txtNum.setText("2");
                break;

I would like to know how to keep the current value and add a new value to the side.

1 answer

2


Assuming that your object txtNum be a TextView, you can use the method append.

txtNum.append("1");

If txtNum is an object that does not have a function append, you can always do something like x = x + y, ex:

txtNum.setText(txtNum.getText() + "1");
  • thanks a lot, I didn’t know the append

  • how can I do to delete the last typed?

  • 1

    I don’t know a function for that, it would have to be manual. If inserting only one character at a time is easier, you can remove with obj.setText(obj.gettext().substring(0, obj.gettext().length() - 2);. Already if it is possible to insert any type of text, then you could have a visible for the last insertion and use this variable together with lastIndexOf() to do the substring.

  • worked perfectly only had to change a thingsbj.setText(obj.gettext(). subSequence(0, obj.gettext(). length() - 1));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.