How to set ontime user input text from EditView to TextView without clicking the button

huangapple go评论61阅读模式
英文:

How to set ontime user input text from EditView to TextView without clicking the button

问题

以下是您要求的翻译内容:

这是我的练习,我在一个地方卡住了。我想将EditText中的实时用户输入文本设置到TextView中,但我不知道如何做到这一点,所以我在这里搜索了一下,找到了这个如何在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>

这是我的Java代码:

package com.example.ontimedata;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView tv1, tv2;
    EditText et1, et2;
    String data1, data2;

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

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

        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:");
                data1 = et1.getText().toString();
            }
        };
        tv1.addTextChangedListener(textWatcher);
        Log.d("TAG", "onCreate:");

        TextWatcher textWatcher1 = 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) {
                data2 = et2.getText().toString();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        tv2.addTextChangedListener(textWatcher1);
    }
}

提前感谢您的回答。

英文:

I was doing practice and I am stuck at one point that I want to set ontime user input text from EditText to TextView but I don't know how can I do this so I search it here and I found this How to get EditText value to TextView at the time of typing without button click in android [closed]

I follow this but it is not working so that I decided to post a new question to know if any improvement is there or not.

here is my simple XML

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;#fff&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;10dp&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_horizontal&quot;
android:layout_marginTop=&quot;40dp&quot;
android:text=&quot;Hello World!&quot;
android:textColor=&quot;#000&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tv2&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_horizontal&quot;
android:layout_marginTop=&quot;20dp&quot;
android:text=&quot;Hello World!&quot;
android:textColor=&quot;#000&quot;
android:textSize=&quot;20sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et1&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:inputType=&quot;text&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;20dp&quot;
android:inputType=&quot;text&quot; /&gt;
&lt;/LinearLayout&gt;

This is my java code

package com.example.ontimedata;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1, tv2;
EditText et1, et2;
String data1, data2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(&quot;TAG&quot;, &quot;beforeTextChanged: &quot;);
data1 = et1.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(&quot;TAG&quot;, &quot;onTextChanged: &quot;);
data1 = et1.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
Log.d(&quot;TAG&quot;, &quot;afterTextChanged: &quot;);
data1 = et1.getText().toString();
}
};
tv1.addTextChangedListener(textWatcher);
Log.d(&quot;TAG&quot;, &quot;onCreate: &quot;);
TextWatcher textWatcher1 = 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) {
data2 = et2.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
}
};
tv2.addTextChangedListener(textWatcher1);
}
}

Thanks in advance if you answered this.

答案1

得分: 2

在您的代码中,您将监听器设置给了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); // 将TextView的文本设置为EditText内的文本
            }
        };

        et1.addTextChangedListener(textWatcher); // 将监听器添加到EditText上
    }
}
英文:

In Your code You set listener to TextView (tv1), You have to add it to EditText (et1). You can do it like this:

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(&quot;MyTag&quot;,
&quot;beforeTextChanged: &quot;
);
}
@Override
public void onTextChanged(
CharSequence s,
int start,
int before,
int count
)
{
Log.d(&quot;MyTag&quot;,
&quot;onTextChanged: &quot;
);
}
@Override
public void afterTextChanged(Editable s)
{
Log.d(&quot;MyTag&quot;,
&quot;afterTextChanged: &quot;
);
tv1.setText(s); // set TextView text to Text inside EditText
}
};
et1.addTextChangedListener(textWatcher); // add Listener to EditText
}
}

答案2

得分: 0

"Ontime user input" 是什么?但如果您希望在编辑文本时更改您的 textView。只需在 onTextChanged() 中执行以下操作:

public void onTextChanged(CharSequence s, int start, int before, int count) {
    textView.setText(editText.getText().toString().trim());
}
英文:

Explain briefly. "Ontime user input" is what?
But if you want to change your textView while you type inside your editText.
Just do this inside onTextChanged():

public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setText(editText.getText().toString().trim());
}

答案3

得分: 0

只需在从文本监听器获取文本后更改您的TextView,需要类似以下的代码:

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);
    }
};
英文:

Just change your TextView after getting text from text watcher, need somthing like this:

TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(&quot;TAG&quot;, &quot;beforeTextChanged: &quot;);
//data1 = et1.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(&quot;TAG&quot;, &quot;onTextChanged: &quot;);
//data1 = et1.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
Log.d(&quot;TAG&quot;, &quot;afterTextChanged: &quot;);
String text = s.toString();
tv1.setText(text)
}
};

huangapple
  • 本文由 发表于 2020年10月13日 15:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64330727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定