How to reflect (display) characters in TextView when writing characters with EditText. This can be achieved by using TextWatcher.
public class MainActivity extends AppCompatActivity implements TextWatcher{
...
}
Implement TextWatcher in the Activity or Fragment you want to display.
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
}
1.beforeTextChanged(CharSequence s, int start, int count,int after) -Method called just before the string is modified ・ Char Sequence s → The character string currently entered in EditText ・ Int start → Start position of the newly added character string in the character string of s ・ Int count → Total number of changed strings in the s string
2.onTextChanged(CharSequence s, int start, int before, int count) ・ Called when one character is entered ・ Char Sequence s → The character string currently entered in EditText ・ Int start → Start position of the newly added character string in the character string of s ・ Int before → Number of existing strings to be deleted ・ Int count → Number of newly added character strings
3.afterTextChanged(Editable s) -Finally this method is called ・ Editable s → The final modifiable, modified string
You can set before, during, and after character input in real time.
addTextChangedListener(this);
Register the target EditText in the listener.
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements TextWatcher {
    EditText editText;
    TextView textView;
    EditText EditText_OUT;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        editText = findViewById(R.id.editText);
        EditText_OUT = (EditText) findViewById(R.id.editText);
        //Register listener
        EditText_OUT.addTextChangedListener(this);
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    @Override
    public void afterTextChanged(Editable s) {
        //Reflect the value entered in TextView in real time
        textView.setText(s);
    }
}
activity.main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.308" />
    <EditText
        android:id="@+id/editText"
        android:layout_width="284dp"
        android:layout_height="79dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:textSize="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

Recommended Posts