I can’t get a Textview to display the content in an Edittext!

Asked

Viewed 514 times

0

I created an app where in its layout contains a Edittext and a Textview.

The initial content of Textview is: "nameless".

inserir a descrição da imagem aqui

I would like the content of Textview was changed when I inserted something into Edittext, what does not occur:

inserir a descrição da imagem aqui

I used a string to capture the content of Edittext to the Textview display its contents, as I will also need to export to a next Activity. So it doesn’t serve me something direct like:

TextView.setText(editText), but yes TextView.setText(String).

Mainactivity.java:

package genesysgeneration.ettotv;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText etNome;
    private TextView tvNome;
    private String nome;

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

        etNome=(EditText)findViewById(R.id.etNome);
        tvNome=(TextView)findViewById(R.id.tvNome);

        if (etNome.getText().length()==0){

            nome="SEM NOME";
            tvNome.setText(nome);

        }else {

            nome=etNome.getText().toString();
            tvNome.setText(nome);

        }

    }
}

1 answer

2


One way to do this is to use the method addTextChangeListener(). Just below your conditions, add the code below:

etNome.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

              tvNome.setText(s.toString());
              nome = s.toString();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Using the class TextWatcher, you have basically three methods:

  • onTextChanged: what is inside it is executed during the text change.

  • afterTextChanged: what is inside it is executed immediately after the text is changed.

  • beforeTextChanged: what has inside it is executed the instant before the text will be changed.

For more details, see in the documentation.

  • Funfou, is now showing the delay of a character in Textview. Type... type the name "Horse" in Edittext, in Textview appears "Caval". Deleting a character or adding there yes appears "Horse", but the contents of Edittext and Textview are never equal.

  • I put on onTextChanged and everything was quiet!!!

  • String value is not changing!!!

  • @Dummy symphore you saw the explanation about onTextChanged right?! Beauty, if it works for you right, just validate the answer. Good luck!

  • string value is not changing!!!

  • @Puppet Symphore put the nome = s.toString(); also within the onTextChanged that will change.

  • @Dummy symphore in this way, its string also receives the value that is inside the EditText

Show 2 more comments

Browser other questions tagged

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