我編寫了一個小型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佈局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>
的應用程序運行,並接受編輯文本,但在那之後,什麼也沒有發生任何幫助非常感謝!
在你的EditText中設置你的'imeOptions =「done」'在xml中,然後看看這個http://stackoverflow.com/questions/5099814/knowing-when-edit-text-is-done-being-edited for設置你的聽衆 – zgc7009
也試過這個以及相同的問題 – zschiff