How to add content to a textview without having to copy what's already in it?

1

Galera is as follows. I would like to know how to add a paragraph or a single word to a textview that already contains content without necessarily copying what already has in it. I would like you to show me something that would suit any text.

An example of what I want exactly:

Ifthecheckboxischeckedthetextdisplayedis"Neo is". Otherwise "Neo is not".

AsIclickthe"+1" button I want it to be added after the content already exists in the textview (Neo is / Neo is not) the status corresponding to the value of the cont variable.

If it contains == 1 = > existing text + single. If it contains == 2 = > existing text + dating. If it contains == 3 = > existing text + married.

package genesysgeneration.wwww;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView tv01;
    private Button btnMais1;
    private int cont=0;
    private CheckBox cbNeo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv01=(TextView)findViewById(R.id.tv01);

        addCheckBoxCondiction();

        addComplementText();

        btnMais1=(Button)findViewById(R.id.btnMais1);
        btnMais1.setOnClickListener(this);

    }
    public void addCheckBoxCondiction(){

        cbNeo=(CheckBox)findViewById(R.id.cbNeo);
        cbNeo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked()){

                    tv01.setText(String.valueOf("Neo está"));

                }else{

                    tv01.setText(String.valueOf("Neo não está"));

                }

            }
        });

    }

    public void addComplementText(){

        if(cont==1){

            //tv01 => texto já existente + solteiro

        }

        if(cont==2){

            //tv01 => texto já existente + namorando

        }

        if(cont==3){

            //tv01 => texto já existente + casado

        }

    }

    public void onClick(View v){

        cont+=1;

    }

}
    
asked by anonymous 14.01.2017 / 23:23

1 answer

2
TextView tv;

tv.setText(
    tv.getText().toString() +
    "seu novo texto"
);

I do not understand if this is exactly what you need, but in this way you will concatenate the String that is already inside the TextView with your new text.

In particular, I do not think this is the most elegant solution, I recommend you do some formatting class that receives some standard inputs and generates the text for you, because in the long run this current strategy becomes more difficult to maintain.

    
14.01.2017 / 23:50