如果用户使用 Android TextWatcher 输入新值,如何更新最终值

How to Update the final value if user enters new Value with Android TextWatcher

由于我是 android 的新手,所以我只是尝试学习一些东西。如果我问的是愚蠢的问题,请原谅。
这就是我的 android 应用程序的样子。

我添加了一个新的 edittext 字段,用户可以在其中添加一个值(例如 3%),如果他愿意的话。然后用户输入的值(例如 3%)应与结果 TextView 中的总值一起计算,并且该值需要显示在 Sub Value 文本视图字段中。例如,如果最终结果文本视图中的总值为 1000,用户输入 3%,则子值字段显示 30。然后最终结果需要更新为 1030。请帮我解决这个问题。提前致谢

public class AddTwo extends AppCompatActivity {

        EditText edit1, edit2, edit3;
        EditText edit4, edit5, edit6;



        TextView textViewSub1, textViewSub2, textViewSub3, textViewSub4, textViewResult;
        TextView nbtText;

        EditText editNbt, vat;

        Button btnNext;

        /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_add_two);

            btnNext = (Button) findViewById(R.id.button2);

            btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    gotoNextPage();
                }

                private void gotoNextPage() {
                    Intent i = new Intent(AddTwo.this, NewClass.class);
                    startActivity(i);
                }
            });

        /*Vat variables*/
            editNbt = (EditText) findViewById(R.id.Edittext11);


        /*First row variables*/
            edit1 = (EditText) findViewById(R.id.editText1);
            edit2 = (EditText) findViewById(R.id.editText2);
            edit3 = (EditText) findViewById(R.id.editText3);
            textViewSub1 = (TextView) findViewById(R.id.TextViewsub1);

        /*Second row variables*/
            edit4 = (EditText) findViewById(R.id.editText5);
            edit5 = (EditText) findViewById(R.id.editText6);
            edit6 = (EditText) findViewById(R.id.editText7);
            textViewSub2 = (TextView) findViewById(R.id.TextViewsub2);

        /*Final Total TextView variable*/
            textViewResult = (TextView) findViewById(R.id.textView_result);

        /*NBT value variable*/
            nbtText = (TextView) findViewById(R.id.textViewNbt12);

            edit1.addTextChangedListener(new LashCustomTextWatcher());
            edit2.addTextChangedListener(new LashCustomTextWatcher());
            edit3.addTextChangedListener(new LashCustomTextWatcher());
            edit4.addTextChangedListener(new LashCustomTextWatcher());
            edit5.addTextChangedListener(new LashCustomTextWatcher());
            edit6.addTextChangedListener(new LashCustomTextWatcher());
            textViewResult.addTextChangedListener(new LashCustomTextWatcher());
        }

        public class LashCustomTextWatcher implements TextWatcher {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                textViewResult.setText(lashCalculate());
            }

            @Override
            public void afterTextChanged(Editable editable) {
                textViewResult.setText(lashCalculate());
            }
        }

        public String lashCalculate() {
            //declaring variables
            double row1_value = 0;
            double row2_value = 0;


            //Optional Values
            double row_nbt_value = 0;
            double row1_value_new = 0;

            DecimalFormat df = new DecimalFormat("0.00##");

            //calculate first row
            if (!edit1.getText().toString().equals("") && !edit2.getText().toString().equals("")) {
                double num1 = Double.parseDouble((edit1.getText().toString()));
                double num2 = Double.parseDouble((edit2.getText().toString()));

                row1_value = num1 * num2;

                double num3 = 0;
                if (!edit3.getText().toString().equals("")) {
                    num3 = Double.parseDouble((edit3.getText().toString()));
                    row1_value = (((100 - num3) * num2) * num1) / 100;
                }

                textViewSub1.setText(df.format(row1_value));
            }

            //calculate second row
            if (!edit4.getText().toString().equals("") && !edit5.getText().toString().equals("")) {
                double num4 = Double.parseDouble((edit4.getText().toString()));
                double num5 = Double.parseDouble((edit5.getText().toString()));

                row2_value = num4 * num5;

                double num6 = 0;
                if (!edit6.getText().toString().equals("")) {
                    num6 = Double.parseDouble((edit6.getText().toString()));
                    row2_value = (((100 - num6) * num5) * num4) / 100;
                }

                textViewSub2.setText(df.format(row2_value));
            }


            /* ========================================================================== */
            //calculate the NBT
            double numNbt = 0;
            if (!editNbt.getText().toString().equals("")) {
                numNbt = Double.parseDouble(editNbt.getText().toString());
                row_nbt_value =  numNbt / 100;

                if (row1_value !=0){
                       row1_value_new =  row1_value * row_nbt_value;
                }
                nbtText.setText(df.format(row1_value_new));
            }

            return df.format(row1_value_new + row2_value);
        }
    }

这是我的 LogCat:

09-12 09:55:51.908 1624-1624/? I/InstallerConnection: connecting... 09-12 09:55:51.936 1624-1624/? I/InstallerConnection: disconnecting... 09-12 09:55:51.978 1624-1624/system_process I/InstallerConnection: connecting... 09-12 09:56:04.447 2433-2502/com.google.android.gms I/FA-SVC: App measurement is starting up, version: 11055 09-12 09:56:04.530 2433-2922/com.google.android.gms I/FA-SVC: This instance being marked as an uploader 09-12 09:56:04.794 2433-2441/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/networkstatistics.sqlite' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 09-12 09:56:14.889 2433-2441/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db.16' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 09-12 09:56:14.890 2433-2441/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db.18' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed. 09-12 09:56:14.892 2433-2441/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

add this line in your code [updated]

    editNbt.addTextChangedListener(new LashCustomTextWatcher()); 


change this code in lashCalculate() method
//calculate the NBT


 double numNbt = 0;
    if (!TextUtils.isEmpty(editNbt.getText().toString())) {
        numNbt = Double.parseDouble(editNbt.getText().toString());
        row_nbt_value = numNbt / 100;

        if (row1_value != 0) {
            row1_value_new = row1_value * row_nbt_value;
        }
        nbtText.setText(df.format(row1_value_new));
    } else {
        row1_value_new = row1_value;
    }

    return df.format(row1_value_new + row2_value);

这是XML代码

<?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


        <!--<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">-->

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="12dp"
            android:layout_marginRight="12dp"
            android:layout_toLeftOf="@+id/textView"
            android:layout_toStartOf="@+id/textView"
            android:ems="3"
            android:gravity="center"
            android:text="Col 1"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/editText5"
            android:layout_alignRight="@+id/editText5"
            android:layout_below="@+id/textView4"
            android:layout_marginTop="19dp"
            android:layout_toLeftOf="@+id/editText2"
            android:ems="3"
            android:inputType="numberDecimal"
            android:layout_alignLeft="@+id/editText5"
            android:layout_alignStart="@+id/editText5" />


        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="3"
            android:inputType="numberDecimal"
            android:layout_alignBaseline="@+id/editText1"
            android:layout_alignBottom="@+id/editText1"
            android:layout_toRightOf="@+id/textView3"
            android:layout_toEndOf="@+id/textView3" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="13dp"
            android:layout_marginRight="13dp"
            android:layout_toLeftOf="@+id/editText3"
            android:layout_toStartOf="@+id/editText3"
            android:ems="4"
            android:gravity="center"
            android:text="Col 2"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/textView_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="63dp"
            android:text="Final Result"
            android:textColor="#232123"
            android:textSize="18dip"
            android:layout_above="@+id/button2"
            android:layout_alignRight="@+id/TextViewsub2"
            android:layout_alignEnd="@+id/TextViewsub2"
            android:layout_marginRight="17dp"
            android:layout_marginEnd="17dp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/textView4"
            android:layout_toStartOf="@+id/textView4"
            android:ems="4"
            android:gravity="center"
            android:text="Col 3"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/editText2"
            android:layout_alignBottom="@+id/editText2"
            android:layout_toLeftOf="@+id/TextViewsub1"
            android:layout_toStartOf="@+id/TextViewsub1"
            android:ems="4"
            android:hint=""
            android:inputType="numberDecimal" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/editText3"
            android:layout_toRightOf="@+id/editText3"
            android:ems="4"
            android:text="Sub Total"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextViewsub1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editText3"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/editText3"
            android:layout_marginEnd="14dp"
            android:layout_marginRight="14dp"
            android:ems="3"
            android:gravity="bottom"
            android:hint="Sub 1"
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editText1"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignTop="@+id/editText1"
            android:ems="2"
            android:gravity="bottom"
            android:text="First Row"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="13dp"
            android:ems="3"
            android:text="Second Row"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editText5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="3"
            android:hint=""
            android:inputType="textPersonName"
            android:layout_alignBottom="@+id/textView6"
            android:layout_toLeftOf="@+id/editText6"
            android:layout_toStartOf="@+id/editText6" />

        <EditText
            android:id="@+id/editText6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/editText2"
            android:layout_alignLeft="@+id/editText2"
            android:layout_alignRight="@+id/editText2"
            android:layout_alignStart="@+id/editText2"
            android:layout_alignTop="@+id/textView6"
            android:ems="3"
            android:hint=""
            android:inputType="textPersonName" />

        <EditText
            android:id="@+id/editText7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/editText5"
            android:layout_alignBottom="@+id/editText5"
            android:layout_alignLeft="@+id/editText3"
            android:layout_alignStart="@+id/editText3"
            android:layout_toLeftOf="@+id/TextViewsub2"
            android:ems="3"
            android:hint=""
            android:inputType="textPersonName" />

        <TextView
            android:id="@+id/TextViewsub2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/editText7"
            android:layout_alignEnd="@+id/TextViewsub1"
            android:layout_alignLeft="@+id/TextViewsub1"
            android:layout_alignRight="@+id/TextViewsub1"
            android:layout_alignStart="@+id/TextViewsub1"
            android:layout_alignTop="@+id/editText7"
            android:ems="3"
            android:gravity="bottom"
            android:hint="Sub 2"
            android:inputType="textPersonName" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:text="next Page"
            android:layout_alignParentBottom="true"
            android:layout_alignLeft="@+id/textView_result"
            android:layout_alignStart="@+id/textView_result" />












        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total Value"
            android:textAppearance="?android:textAppearanceMedium"
            android:textStyle="bold"
            android:layout_alignBaseline="@+id/textView_result"
            android:layout_alignBottom="@+id/textView_result"
            android:layout_toLeftOf="@+id/editText7"
            android:layout_toStartOf="@+id/editText7" />

        <TextView
            android:id="@+id/textView11"
            android:inputType="textMultiLine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="65dp"
            android:text="Add Value (Optional)"
            android:layout_below="@+id/editText5"
            android:layout_alignRight="@+id/textView"
            android:layout_alignEnd="@+id/textView" />

        <EditText
            android:id="@+id/editText9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/textView11"
            android:layout_alignLeft="@+id/editText7"
            android:layout_alignStart="@+id/editText7"
            android:ems="10"
            android:inputType="textPersonName"
            android:hint="%"
            android:gravity="center"
            android:layout_toLeftOf="@+id/TextViewsub2"
            android:layout_toStartOf="@+id/TextViewsub2" />

        <TextView
            android:id="@+id/editText13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:hint="Sub value"
            android:gravity="bottom"
            android:layout_alignTop="@+id/editText9"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_toRightOf="@+id/editText9"
            android:layout_toEndOf="@+id/editText9"
            android:layout_alignBottom="@+id/editText9" />


    </RelativeLayout>

        <!--</RelativeLayout>-->