如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮
Posted
技术标签:
【中文标题】如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮【英文标题】:How to set multiple value from EditText to 1 Textview and Automaticaly show without click Button 【发布时间】:2019-11-17 16:56:24 【问题描述】:是否可以将多个从 EditText 设置为 1 Textview 并自动显示而无需单击任何按钮。这是 XML 示例。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_
android:layout_
android:layout_below="@+id/labelnim"
android:layout_marginTop="7dp"
android:text="Name :"
android:textSize="18sp" />
<EditText
android:id="@+id/name"
android:layout_
android:layout_
android:inputType="text" >
</EditText>
<TextView
android:layout_
android:layout_
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Spesification"
android:textSize="18sp" />
<EditText
android:id="@+id/spesification"
android:layout_
android:layout_
android:inputType="text"
>
</EditText>
<TextView
android:id="@+id/TextView Output"
android:layout_
android:layout_
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Output"
android:textSize="18sp" />
</LinearLayout>
我希望 TextViewOutput 中的数据与 Name + Specification 一样显示。此外,当我输入时,输出会自动更改为每当我输入 Both EditText 时
【问题讨论】:
是的,您可以使用addTextChangedListener
来佩戴它,但是很难在同一个视图中管理两个输出,所以我建议您在水平视图中使用两个 textView,例如 output1 和 putput2
看我的回答..对你有帮助
【参考方案1】:
是的,当然,您必须使用addTextChangedListener
遵循以下代码
editText.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)
String editValue = editText.getText().toString();
if(editValue.lenght!=0)
String textValue = yourTextView.getText().toString();
int val = Integer.parse(textValue);
int edVal = Integer.parse(editValue);
int result = edVal * val;
resultText.setText(result);
@Override
public void afterTextChanged(Editable s)
);
希望这会对你有所帮助。
【讨论】:
【参考方案2】:很难在同一个 textView 中维护两个不同的 addOnTextChangeListerner's
输出,因此最好将布局部分替换 -
<LinearLayout
android:layout_
android:layout_
android:layout_below="@+id/labelnama"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_Output1"
android:layout_
android:layout_
android:layout_marginTop="25dp"
android:layout_marginRight="2dp"
android:text="Output"
android:textSize="18sp" />
<TextView
android:id="@+id/textView_Output2"
android:layout_
android:layout_
android:layout_marginTop="25dp"
android:text=""
android:textSize="18sp" />
</LinearLayout>
并在两个 editText 上应用 addTextChangedListener
并在各自的 TextViews 中设置它们的输出......像这样
final EditText nameEditText = (EditText) findViewById(R.id.name);
final EditText spesificationEditText = (EditText) findViewById(R.id.spesification);
final TextView textViewOutput1 = (TextView) findViewById(R.id.textView_Output1);
final TextView textViewOutput2 = (TextView) findViewById(R.id.textView_Output2);
nameEditText.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)
String name = nameEditText.getText().toString();
if(name!=null)
textViewOutput1.setText(name);
@Override
public void afterTextChanged(Editable s)
);
spesificationEditText.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)
String specification = spesificationEditText.getText().toString();
if(specification!=null)
textViewOutput2.setText(specification);
@Override
public void afterTextChanged(Editable s)
);
【讨论】:
【参考方案3】:谢谢你帮助我。你们都是一个了不起的人。这是代码
EditText 的前 2 个或更多值显示在 1 个 Textview 中
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
对于自动文本,您使用 TextWatcher 是对的
Name.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)
String name = NamaAlat.getText().toString();
if(name!=null)
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
@Override
public void afterTextChanged(Editable s)
);
Spesification.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)
String spek = NamaAlat.getText().toString();
if(spek!=null)
TextView output.setText(Name.getText().toString()+"-"+Spesicification.getText().toString()"-"+MoreOuput.getText().toString());
@Override
public void afterTextChanged(Editable s)
);
非常感谢。
【讨论】:
如果答案是有帮助的..upvote答案并将其标记为已验证以上是关于如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮的主要内容,如果未能解决你的问题,请参考以下文章