TextWatcher的问题

时间:2016-01-19 15:59:07

标签: java android textwatcher android-textwatcher

我似乎遇到了一些textwatcher的问题,作为java / android开发的新手我有几个问题。简而言之,我有3个字段,2个可编辑和1个视图。我们的想法是改变两个字段并让第三个字段减去它们。

  1. 以下代码似乎运行良好,但即使编辑字段textwatcher正在观看,该字段似乎也无法更改。

  2. 有没有办法观看多个字段?理想情况下,我希望两个字段都被观看,并在编辑时改变第三个字段。

  3. 有没有更好的方法让前两个编辑字段不为null而不使用try / catch?如果我不使用try / catch,应用程序就会崩溃。

  4. 感谢您的帮助。

    package com.autotools.autotools;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.widget.EditText;
    import android.widget.TextView;
    
    public class autoLoan extends AppCompatActivity {
    
    
    double TotalAfterTrade, FinalAutoPrice, TradeInPrice;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_loan);
    
        final TextView PriceAfterTradeText = (TextView) findViewById(R.id.textViewSubTotal);
        EditText FinalAutoPriceText = (EditText) findViewById(R.id.editTextPrice);
    
        try {
            EditText TradeInPriceText = (EditText) findViewById(R.id.editTextTrade);
            FinalAutoPrice = Double.parseDouble(FinalAutoPriceText.getText().toString());
            TradeInPrice = Double.parseDouble(TradeInPriceText.getText().toString());
            TotalAfterTrade = FinalAutoPrice - TradeInPrice;
    
    
        } catch (NumberFormatException e) {
        }
    
    
          FinalAutoPriceText.addTextChangedListener(new TextWatcher() {
    
              public void afterTextChanged(Editable s) {
              }
    
              public void beforeTextChanged(CharSequence s, int start,
                                            int count, int after) {
              }
    
              public void onTextChanged(CharSequence s, int start,
                                        int before, int count) {
                  PriceAfterTradeText.setText(String.valueOf(TotalAfterTrade));
              }
          });
    
    }
    }
    

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.autotools.autotools.autoLoan">
    
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editTextPrice"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="80dp"
        android:text=""
        android:textSize="25dp" />
    
    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Price of Vehicle"
        android:id="@+id/textViewPrice"
        android:layout_alignBottom="@+id/editTextPrice"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="25dp" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Auto Loan Calculator"
        android:id="@+id/textView3"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignEnd="@+id/textView2"
        android:layout_above="@+id/textView2"
        android:textSize="35dp"
        android:layout_marginTop="20dp"
        android:textAlignment="center" />
    
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/editTextTrade"
        android:layout_marginTop="40dp"
        android:text=""
        android:textSize="25dp"
        android:layout_below="@+id/textViewPrice"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Trade-in Value"
        android:id="@+id/textViewTrade"
        android:textSize="25dp"
        android:layout_alignBottom="@+id/editTextTrade"
        android:layout_alignLeft="@+id/textViewPrice"
        android:layout_alignStart="@+id/textViewPrice" />
    
    <TextView
        android:layout_width="175dp"
        android:textSize="25dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="0"
        android:id="@+id/textViewSubTotal"
        android:layout_below="@+id/editTextTrade"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="60dp"
        android:textAlignment="center"
        android:layout_alignRight="@+id/editTextTrade"
        android:layout_alignEnd="@+id/editTextTrade" />
    
    <TextView
        android:layout_width="175dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Price after Trade"
        android:id="@+id/textViewPriceAfterTrade"
        android:textSize="23dp"
        android:layout_alignTop="@+id/textViewSubTotal"
        android:layout_alignLeft="@+id/textViewTrade"
        android:layout_alignStart="@+id/textViewTrade" />
    
    
    </RelativeLayout>
    

1 个答案:

答案 0 :(得分:1)

它不会改变,因为你总是放在你在onCreate传递给它的相同值里面

 TotalAfterTrade = FinalAutoPrice - TradeInPrice;

这个价值永远不会改变。

要更改textField中的值,请使用以下代码:

        public void onTextChanged(CharSequence s, int start,
                                    int before, int count) {
        FinalAutoPrice = Double.parseDouble(FinalAutoPriceText.getText().toString());
        TradeInPrice = Double.parseDouble(TradeInPriceText.getText().toString());
        TotalAfterTrade = FinalAutoPrice - TradeInPrice;
        PriceAfterTradeText.setText(String.valueOf(TotalAfterTrade));
       }