【问题标题】:how to update the same edittext while using Textwatcher?使用 Textwatcher 时如何更新相同的编辑文本?
【发布时间】:2020-04-19 19:39:15
【问题描述】:

当我在 input_edittext 中输入值时,它会相乘并在显示后显示结果,当我编辑 inputedittext 应用程序终止时,请告诉我要放入 aftertext 更改侦听器中的代码是什么,它应该编辑数字并再次显示结果。

请任何人解决这个问题

谢谢


    <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);

    }

【问题讨论】:

  • 请同时分享异常日志
  • 分享你的错误日志。
  • 错误只是字符串为空

标签: android image textview android-edittext textwatcher


【解决方案1】:

当您调用 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);
}

【讨论】:

  • 我有 20 个输入值,最后我有总计按钮,总计应该显示在下一个活动中,有 20 个输入结果值我该怎么做
猜你喜欢
  • 2012-03-18
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 2016-06-02
  • 2012-08-29
  • 2012-01-15
相关资源
最近更新 更多