Java 如何在不单击按钮的情况下将实时用户输入文本从EditView设置为TextView

Java 如何在不单击按钮的情况下将实时用户输入文本从EditView设置为TextView,java,android,textview,android-edittext,ontime,Java,Android,Textview,Android Edittext,Ontime,我在做练习,我被困在一个点上,我想设置实时用户输入文本从EditText到TextView,但我不知道如何才能做到这一点,所以我在这里搜索,我找到了这个 我遵循这一点,但它不起作用,所以我决定发布一个新问题,以了解是否有任何改进 下面是我的简单XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android

我在做练习,我被困在一个点上,我想设置实时用户输入文本从EditText到TextView,但我不知道如何才能做到这一点,所以我在这里搜索,我找到了这个 我遵循这一点,但它不起作用,所以我决定发布一个新问题,以了解是否有任何改进

下面是我的简单XML

<LinearLayout 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"
android:background="#fff"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="40dp"
    android:text="Hello World!"
    android:textColor="#000"
    android:textSize="20sp" />

<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20dp"
    android:text="Hello World!"
    android:textColor="#000"
    android:textSize="20sp" />

<EditText
    android:id="@+id/et1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:inputType="text" />

<EditText
    android:id="@+id/et2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:inputType="text" />

</LinearLayout>

如果您回答了此问题,请提前感谢。

在代码中将listener设置为TextView(
tv1
),您必须将其添加到EditText(
et1
)。您可以这样做:

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    TextView tv1;
    EditText et1;

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

        tv1 = findViewById(R.id.tv1);
        et1 = findViewById(R.id.et1);

        TextWatcher textWatcher = new TextWatcher()
        {
            @Override
            public void beforeTextChanged(
                    CharSequence s,
                    int start,
                    int count,
                    int after
            )
            {
                Log.d("MyTag",
                      "beforeTextChanged: "
                );
            }

            @Override
            public void onTextChanged(
                    CharSequence s,
                    int start,
                    int before,
                    int count
            )
            {
                Log.d("MyTag",
                      "onTextChanged: "
                );
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                Log.d("MyTag",
                      "afterTextChanged: "
                );

                tv1.setText(s); // set TextView text to Text inside EditText
            }
        };

        et1.addTextChangedListener(textWatcher); // add Listener to EditText
    }
}
TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Log.d("TAG", "beforeTextChanged: ");
        //data1 = et1.getText().toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d("TAG", "onTextChanged: ");
        //data1 = et1.getText().toString();
    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.d("TAG", "afterTextChanged: ");
        String text = s.toString();
        tv1.setText(text)
    }
};
简单解释一下。“实时用户输入”是什么? 但如果您想在editText中键入内容时更改textView。 只需在onTextChanged()中执行此操作:


从文本观察程序获取文本后,只需更改文本视图,需要以下内容:

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity
{
    TextView tv1;
    EditText et1;

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

        tv1 = findViewById(R.id.tv1);
        et1 = findViewById(R.id.et1);

        TextWatcher textWatcher = new TextWatcher()
        {
            @Override
            public void beforeTextChanged(
                    CharSequence s,
                    int start,
                    int count,
                    int after
            )
            {
                Log.d("MyTag",
                      "beforeTextChanged: "
                );
            }

            @Override
            public void onTextChanged(
                    CharSequence s,
                    int start,
                    int before,
                    int count
            )
            {
                Log.d("MyTag",
                      "onTextChanged: "
                );
            }

            @Override
            public void afterTextChanged(Editable s)
            {
                Log.d("MyTag",
                      "afterTextChanged: "
                );

                tv1.setText(s); // set TextView text to Text inside EditText
            }
        };

        et1.addTextChangedListener(textWatcher); // add Listener to EditText
    }
}
TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        Log.d("TAG", "beforeTextChanged: ");
        //data1 = et1.getText().toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Log.d("TAG", "onTextChanged: ");
        //data1 = et1.getText().toString();
    }

    @Override
    public void afterTextChanged(Editable s) {
        Log.d("TAG", "afterTextChanged: ");
        String text = s.toString();
        tv1.setText(text)
    }
};