0

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

  1. 下面的代码似乎运行良好,但该字段似乎永远不会改变,即使编辑字段 textwatcher 正在观看。

  2. 有没有办法查看多个字段?理想情况下,我希望两个字段都被监视并在编辑任何一个字段时更改第三个字段。

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

谢谢你的帮助。

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>
4

1 回答 1

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));
       }
于 2016-01-19T16:10:53.940 回答