如果用户使用Android TextWatcher

时间:2017-09-12 07:58:53

标签: android android-edittext user-input textwatcher

因为我对android很新,所以我只是尝试学习东西。如果我问愚蠢的问题,请原谅 这就是我的Android应用程序的样子。

我添加了一个新的edittext字段,用户可以在其中添加类似(例如3%)的值,如果他喜欢的话。然后,用户输入的值(例如3%)应使用结果TextView中的总值进行计算,该值需要显示在“子值”文本视图字段中。例如,如果最终结果文本视图中的总值为1000且用户输入为3%,则“子值”字段将显示30.然后“最终结果”需要更新为1030.请帮我解决此问题。提前致谢

enter image description here

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:连接......   09-12 09:55:51.936 1624-1624 /? I / InstallerConnection:断开连接......   09-12 09:55:51.978 1624-1624 / system_process I / InstallerConnection:连接......   09-12 09:56:04.447 2433-2502 / com.google.android.gms I / FA-SVC:应用程序测量正在启动,版本:11055   09-12 09:56:04.530 2433-2922 / com.google.android.gms I / FA-SVC:此实例被标记为上传者   09-12 09:56:04.794 2433-2441 / com.google.android.gms W / SQLiteConnectionPool:数据库' /data/user/0/com.google.android.gms/databases/networkstatistics的SQLiteConnection对象.sqlite'被泄露了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。   09-12 09:56:14.889 2433-2441 / com.google.android.gms W / SQLiteConnectionPool:数据库的SQLiteConnection对象' /data/user/0/com.google.android.gms/databases/metrics .db.16'被泄露了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。   09-12 09:56:14.890 2433-2441 / com.google.android.gms W / SQLiteConnectionPool:数据库的SQLiteConnection对象' /data/user/0/com.google.android.gms/databases/help_responses .db.18'被泄露了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。   09-12 09:56:14.892 2433-2441 / com.google.android.gms W / SQLiteConnectionPool:数据库的SQLiteConnection对象' /data/user/0/com.google.android.gms/databases/auto_complete_suggestions .db的'被泄露了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。

2 个答案:

答案 0 :(得分:0)

{{1}}

答案 1 :(得分:0)

这是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>-->