如何在使用 Textwatcher 时更新相同的编辑文本?
how to update the same edittext while using Textwatcher?
当我在 input_edittext 中输入值时,当我编辑 inputedittext 应用程序终止时,它乘以并显示结果,请告诉我要在 aftertext change listerner 中放入什么代码,它应该编辑数字并再次显示结果。
请任何人解决这个问题
谢谢
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewitem1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="44dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/teacher" />
<TextView
android:id="@+id/textviewweight1"
android:layout_width="63dp"
android:layout_height="49dp"
android:layout_marginStart="8dp"
android:text="13.3"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageViewitem1"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textViewmulti1"
android:layout_width="52dp"
android:layout_height="35dp"
android:text="*"
android:textAlignment="center"
android:textColor="#000"
android:textSize="27sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageViewitem1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.02">
</TextView>
<TextView
android:id="@+id/textViewequals1"
android:layout_width="52dp"
android:layout_height="35dp"
android:text="="
android:textAlignment="center"
android:textColor="#000"
android:textSize="27sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.641"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.019">
</TextView>
<EditText
android:id="@+id/textViewresult1"
android:layout_width="111dp"
android:layout_height="49dp"
android:background="@drawable/btnclientuse"
android:hint="result"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.979"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
TextWatcher textWatcher= new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Float v1 = Float.parseFloat(t1.getText().toString());
Float v2 = Float.parseFloat(e1.getText().toString());
Float calculateValue1 = (v1*v2);
ee1.setText(calculateValue1.toString());
}
@Override
public void afterTextChanged(Editable editable) {
if(!editable.toString().startsWith("")){
editable.insert(0,"");
}
}
};
e1.addTextChangedListener(textWatcher);
}
当您调用 setText 时,它会再次触发 onTextChanged。
您需要删除 textChangeListener,完成您的工作,然后重新设置它:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ee1.removeTextChangedListener(this);
try {
Float v1 = Float.parseFloat(t1.getText().toString());
Float v2 = Float.parseFloat(e1.getText().toString());
Float calculateValue1 = (v1 * v2);
ee1.setText(calculateValue1.toString());
}
catch(NumberFormatException ex){
//handle exception
}
ee1.addTextChangedListener(this);
}
当我在 input_edittext 中输入值时,当我编辑 inputedittext 应用程序终止时,它乘以并显示结果,请告诉我要在 aftertext change listerner 中放入什么代码,它应该编辑数字并再次显示结果。
请任何人解决这个问题
谢谢
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageViewitem1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="44dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/teacher" />
<TextView
android:id="@+id/textviewweight1"
android:layout_width="63dp"
android:layout_height="49dp"
android:layout_marginStart="8dp"
android:text="13.3"
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageViewitem1"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textViewmulti1"
android:layout_width="52dp"
android:layout_height="35dp"
android:text="*"
android:textAlignment="center"
android:textColor="#000"
android:textSize="27sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageViewitem1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.02">
</TextView>
<TextView
android:id="@+id/textViewequals1"
android:layout_width="52dp"
android:layout_height="35dp"
android:text="="
android:textAlignment="center"
android:textColor="#000"
android:textSize="27sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.641"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.019">
</TextView>
<EditText
android:id="@+id/textViewresult1"
android:layout_width="111dp"
android:layout_height="49dp"
android:background="@drawable/btnclientuse"
android:hint="result"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.979"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
TextWatcher textWatcher= new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Float v1 = Float.parseFloat(t1.getText().toString());
Float v2 = Float.parseFloat(e1.getText().toString());
Float calculateValue1 = (v1*v2);
ee1.setText(calculateValue1.toString());
}
@Override
public void afterTextChanged(Editable editable) {
if(!editable.toString().startsWith("")){
editable.insert(0,"");
}
}
};
e1.addTextChangedListener(textWatcher);
}
当您调用 setText 时,它会再次触发 onTextChanged。 您需要删除 textChangeListener,完成您的工作,然后重新设置它:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
ee1.removeTextChangedListener(this);
try {
Float v1 = Float.parseFloat(t1.getText().toString());
Float v2 = Float.parseFloat(e1.getText().toString());
Float calculateValue1 = (v1 * v2);
ee1.setText(calculateValue1.toString());
}
catch(NumberFormatException ex){
//handle exception
}
ee1.addTextChangedListener(this);
}