在下面的代码中,我试图将两个EditText字段中的数值数据传递给两个变量,执行计算(乘法),然后将结果显示在第三个EditText字段中。
我尝试过两种不同的场景,但每当我尝试运行代码时,程序崩溃了。以下是第一个场景:
package edu.arizona.uas.fonji.bmicalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.text.DecimalFormat;
public class BmiCalculator extends ActionBarActivity {
Button reset_button, calculate_button;
EditText height_text;
EditText weight_text;
EditText bmi_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi_calculator);
EditText height_text = (EditText)findViewById(R.id.height); //get value from height text box
String height_string = height_text.getText().toString(); //convert to string
final float height_value = Integer.parseInt(height_string); //convert to number
EditText weight_text = (EditText)findViewById(R.id.weight); //get value from weight text box
String weight_string = weight_text.getText().toString(); //convert to string
final float weight_value = Integer.parseInt(weight_string); //convert to number
calculate_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
DecimalFormat df = new DecimalFormat("#.0");
double adouble = weight_value*height_value;
bmi_text.setText("Number is " + df.format(adouble));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bmi_calculator, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
由于我试图收集数据并在OnClickListener()之外转换(height_text和weight_text),导致可能没有收集任何数据我在下面的场景中对其进行了修改,并且我还试图保护从运行时异常,如无效输入,如下所示:
package edu.arizona.uas.fonji.bmicalculator;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class BmiCalculator extends ActionBarActivity {
Button reset_button, calculate_button;
EditText height_text;
EditText weight_text;
EditText bmi_text;
TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi_calculator);
calculate_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
float height_value = 0;
float weight_value = 0;
EditText height_text = (EditText)findViewById(R.id.height); //get value from height text box
try{
String height_string = height_text.getText().toString(); //convert to string
height_value = Integer.parseInt(height_string); //convert to number
}catch(Exception e){
myTextView.setText("Error, invalid character");
}
EditText weight_text = (EditText)findViewById(R.id.weight); //get value from weight text box
try{
String weight_string = weight_text.getText().toString(); //convert to string
weight_value = Integer.parseInt(weight_string); //convert to number
}catch(Exception e){
myTextView.setText("Error, Invalid character");
}
DecimalFormat df = new DecimalFormat("#.0");
double adouble = weight_value*height_value;
bmi_text.setText("Number is " + df.format(adouble));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bmi_calculator, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在任何一种情况下,我仍然无法使其正常工作。我该如何解决这个问题? .xml代码如下所示:
<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="edu.arizona.uas.fonji.bmicalculator.BmiCalculator" >
<Button
android:id="@+id/reset"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_above="@+id/calculate"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="reset" />
<Button
android:id="@+id/calculate"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="37dp"
android:text="calculate" />
<EditText
android:id="@+id/height"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="number"
android:hint="height" >
</EditText>
<EditText
android:id="@+id/weight"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/height"
android:layout_below="@+id/height"
android:ems="10"
android:hint="weight"
android:inputType="number"
/>
<EditText
android:id="@+id/bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/weight"
android:layout_below="@+id/weight"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="bmi">
<requestFocus />
</EditText>
<TextView
android:id="@+id/report_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/reset"
android:layout_alignTop="@+id/height"
android:padding="24dp"
/>
</RelativeLayout>