是否可以将多个EditText设置为1个Textview并自动显示而无需单击任何按钮。这是 XML 示例。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnim"
android:layout_marginTop="7dp"
android:text="Name :"
android:textSize="18sp" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Spesification"
android:textSize="18sp" />
<EditText
android:id="@+id/spesification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
>
</EditText>
<TextView
android:id="@+id/TextView Output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/labelnama"
android:layout_marginTop="25dp"
android:text="Output"
android:textSize="18sp" />
</LinearLayout>
我希望数据在 TextViewOutput 中显示为名称 + 规范。另外,当我输入时,输出会自动更改为每当我输入 Both EditText 时
最佳答案
是的,您必须使用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) {
}
});
希望这对您有帮助。
关于java - 如何将 EditText 中的多个值设置为 1 Textview 并自动显示而无需单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56930466/