文本字段不会填充

时间:2016-03-14 17:59:18

标签: java android textfield

我为练习编写了一个小型Android应用程序,但是当我使用EditText字段时,其他文本字段不会填充。找不到问题

package com.zschiff.invoice;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import java.text.NumberFormat;

public class invoice_total_activity extends Activity
implements OnEditorActionListener {
   // define the private widget variables
   private EditText subtotalEditText;
   private TextView discountPercentText;
   private TextView discountTextView;
   private TextView totalTextView;

  //define the instance variables
  private String subtotalString;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_invoice_total_activity);

     //create link to layout
     subtotalEditText = (EditText) findViewById(R.id.subtotalAmountEditText);
     discountPercentText = (TextView) findViewById(R.id.discountPercent);
     discountTextView = (TextView) findViewById(R.id.discountAmount);
     totalTextView = (TextView) findViewById(R.id.totalAmount);

     //set the listeners
     subtotalEditText.setOnEditorActionListener(this);
    }

    public void calculateAndDisplay() {

     //get the subtotal
     subtotalString = subtotalEditText.getText().toString();
     float subtotal;

     if (subtotalString.equals("")) {
         subtotal = 0;
     }
     else {
         subtotal = Float.parseFloat(subtotalString);
     }

     // get discount percent
     float discountPercent;
     if (subtotal >= 200) {
         discountPercent = .2f;
     }
     else if (subtotal >= 100) {
         discountPercent = .1f;
     }

     else {
         discountPercent = 0;
     }


     //calculate discount
     float discountAmount = subtotal * discountPercent;
     float total = subtotal - discountAmount;

     //display results
     NumberFormat percent = NumberFormat.getPercentInstance();
     discountPercentText.setText(percent.format(discountPercent));

     NumberFormat currency = NumberFormat.getCurrencyInstance();
     discountTextView.setText(currency.format(discountAmount));
     totalTextView.setText(currency.format(total));
   }

  @Override
  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

      calculateAndDisplay();

      return false;
   }
}

这是android layout 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.zschiff.invoice.invoice_total_activity">

 <!-- Subtotal -->

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/subtotal_label"
     android:id="@+id/subtotalLabel"
     android:textSize="20sp"
     android:textStyle="bold"
     android:layout_alignParentTop="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:paddingTop="20dp" />

 <EditText
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:inputType="number"
     android:ems="7"
     android:id="@+id/subtotalAmountEditText"
     android:text="@string/subtotal_amount"
     android:textSize="20sp"
     android:layout_alignBottom="@id/subtotalLabel"
     android:layout_alignParentTop="true"
     android:layout_toRightOf="@+id/subtotalLabel"
     android:layout_toEndOf="@+id/subtotalLabel"
     android:layout_marginLeft="100dp" />

 <!-- Discount Percent -->

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/discountPercentLabel"
     android:text="@string/discount_label"
     android:textSize="20sp"
     android:textStyle="bold"
     android:layout_below="@+id/subtotalLabel"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:paddingTop="20dp"/>

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/discountPercent"
     android:text="@string/discount_percent"
     android:textSize="20sp"
     android:layout_toRightOf="@+id/discountPercentLabel"
     android:layout_alignBottom="@+id/discountPercentLabel"
     android:layout_alignLeft="@+id/subtotalAmountEditText"
     android:layout_alignStart="@+id/subtotalAmountEditText" />

 <!-- Discount Amount -->

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/discountAmountLabel"
     android:text="@string/discount_amount_label"
     android:textSize="20sp"
     android:textStyle="bold"
     android:layout_below="@+id/discountPercentLabel"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:paddingTop="20dp" />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/discountAmount"
     android:text="@string/discount_amount"
     android:textSize="20sp"
     android:layout_toRightOf="@id/discountAmountLabel"
     android:layout_alignBottom="@+id/discountAmountLabel"
     android:layout_alignLeft="@+id/discountPercent"
     android:layout_alignStart="@+id/discountPercent" />

 <!-- Total Amount -->

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/totalLabel"
     android:text="@string/total_label"
     android:textSize="20sp"
     android:textStyle="bold"
     android:layout_below="@+id/discountAmountLabel"
     android:layout_alignParentLeft="true"
     android:layout_alignParentStart="true"
     android:paddingTop="20dp" />

 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/totalAmount"
     android:text="@string/total_amount"
     android:textSize="20sp"
     android:layout_toRightOf="@id/totalLabel"
     android:layout_alignBottom="@+id/totalLabel"
     android:layout_alignLeft="@+id/discountAmount"
     android:layout_alignStart="@+id/discountAmount" />
</RelativeLayout>

应用程序运行,并接受编辑文本,但在此之后,没有任何反应,非常感谢!!

2 个答案:

答案 0 :(得分:0)

在EditText字段中输入一些数字后,按Enter键(如果您正在运行模拟器)。我键入一个数字,按下回车键,然后得到以下内容...... enter image description here

答案 1 :(得分:0)

谢谢大家的回答! AVD有问题。我擦除了用户信息,它的工作就像一个魅力。