2017-09-12 70 views
0

由于我对android非常陌生,我只是试着学习一些东西。所以,如果我提出愚蠢的问题,请原谅。
这是我的android应用程序的外观。如何在用户通过Android TextWatcher输入新值时更新最终值

我添加一个新的EditText场,其中用户可以添加像(例如3%),如果他喜欢的值。然后,用户输入的值(例如3%)应该使用Result TextView中的总值进行计算,并且该值需要显示在Sub Value 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:

9月9日至12日:55:51.908 1624年至1624年/? I/InstallerConnection:连接... 09-12 09:55:51.936 1624-1624 /? I/InstallerConnection:断开... 9月9日至12日:55:51.978 1624年至1624年/ system_process I/InstallerConnection:连接... 9月9日至12日:56:04.447 2533至02年/ com.google.android.gms I/FA-SVC:应用测量正在启动,版本:11055 9月9日至十二日: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.sqlite'的SQLiteConnection对象是泄露!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。 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'泄漏了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。 9月9日至12日:56:14.890 2433年至2441年/ com.google.android.gms W/SQLiteConnectionPool:甲SQLiteConnection对象数据库“/data/user/0/com.google.android.gms/databases/help_responses.db .18'被泄露!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。 9月9日至12日:56:14.892 2433年至2441年/ com.google.android.gms W/SQLiteConnectionPool:甲SQLiteConnection对象数据库“/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db '泄漏了!请修复您的应用程序以正确结束正在进行的事务,并在不再需要时关闭数据库。

+0

计算(); –

+0

@J Ramesh,是的我用同样的方法计算,但不起作用,请你给我一个建议 – Lash

+0

你可以请xml吗? –

回答

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

@J Ramesh非常有用,我添加了这一行,但是像以前一样,我的应用程序崩溃并且不能正常工作 – Lash

+0

可以请你崩溃日志吗? –

+0

你的xml不包含textViewNbt12和Edittext11 –

0

这是XML代码中相同的方法lashCalculate

<?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>--> 
+0

@ J Ramesh,我刚刚看到你的评论,我会尽力通知你。谢谢你正在试图帮助我 – Lash

+0

@J Ramesh,它完美的工作,非常感谢,如果我添加这样的另一个领域,它是否以相同的方式工作?如果可以的话,你能否介绍一下你对代码所做的事情 – Lash