Bmi.java
package com.demo.android.bmi;
import
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Bmi extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
setListeners();
//listen for button clicks
//Button button = (Button)findViewById(R.id.submit);
//button.setOnClickListener(calcBMI);
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
//private TextView view_result;
//private TextView view_suggest;
public void findViews(){
button_calc = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.height);
field_weight = (EditText)findViewById(R.id.weight);
//view_result = (TextView)findViewById(R.id.result);
//view_suggest = (TextView)findViewById(R.id.suggest);
}
public void setListeners(){
button_calc.setOnClickListener(calcBMI);
}
/**
*
*/
/*private Button.OnClickListener calcBMI = new Button.OnClickListener(){
public void onClick(View v){
//EditText height = (EditText)findViewById(R.id.height);
//EditText width = (EditText)findViewById(R.id.width);
try{
double h = Double.parseDouble(field_height.getText().toString())/100;
double w = Double.parseDouble(field_width.getText().toString());
double BMI = w/(h*h);
DecimalFormat nf = new DecimalFormat("0.00");
//TextView result = (TextView)findViewById(R.id.result);
//result.setText(R.string.result);
view_result.setText(getText(R.string.result)+nf.format(BMI));
//TextView suggest = (TextView)findViewById(R.id.suggest);
if(BMI>25){
view_suggest.setText(R.string.advice_heavy);
}else if(BMI<20){
view_suggest.setText(R.string.advice_light);
}else{
view_suggest.setText(R.string.advice_average);
}
//openOptionsDialog();
}catch (Exception e) {
// TODO: handle exception
Toast.makeText(Bmi.this, R.string.input_error, Toast.LENGTH_SHORT)
.show();
}
}
};
*/
private Button.OnClickListener calcBMI= new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(Bmi.this, Report.class);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", field_height.getText().toString());
bundle.putString("KEY_WEIGHT", field_weight.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
};
public void openOptionsDialog(){
new AlertDialog.Builder(Bmi.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Uri uri = Uri.parse(R.string.homepage_uri);
try{
//Uri uri = Uri.parse("geo:39.895874,116.321238");
Uri uri = Uri.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}catch(Exception e){
}
}
})
.show();
//Toast.makeText(Bmi.this, R.string.toast_test, Toast.LENGTH_SHORT).show();
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST+1;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_QUIT:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0,MENU_ABOUT,0,R.string.about)
.setIcon(android.R.drawable.ic_menu_help)
.setAlphabeticShortcut('G');
menu.add(0,MENU_QUIT,0,R.string.quit)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/height" />
<EditText android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weight"/>
<EditText android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""/>
<Button android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"/>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="height">身高(cm)</string>
<string name="weight">体重(cm)</string>
<string name="submit">计算BMI的值</string>
<string name="result">你的BMI的值是</string>
<string name="app_name2">BMI</string>
<string name="app_name">BMI计算器</string>
<string name="about_title">关于 Android BMI</string>
<string name="about_msg">Android BMI Calc</string>
<string name="ok_label">确认</string>
<string name="toast_test">BMI计算器</string>
<string name="input_error">打错了吗?智能输入数字哦!</string>
<string name="homepage_label">首页</string>
<string name="homepage_uri">http://www.baidu.com</string>
<string name="about">关于…</string>
<string name="quit">退出</string>
</resources>
advice.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="advice_light">你该多吃点</string>
<string name="advice_average">体形很棒哦</string>
<string name="advice_heavy">你该考虑节食了</string>
</resources>
Report.java
package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Report extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
showResults();
setListeners();
}
private Button button_back;
private TextView view_result;
private TextView view_suggest;
private void findViews(){
button_back = (Button)findViewById(R.id.report_back);
view_result = (TextView)findViewById(R.id.result);
view_suggest = (TextView)findViewById(R.id.suggest);
}
private void showResults(){
DecimalFormat df = new DecimalFormat("0.00");
Bundle bunde = this.getIntent().getExtras();
double height = Double.parseDouble(bunde.getString("KEY_HEIGHT"))/100;
double weight = Double.parseDouble(bunde.getString("KET_WEIGHT"));
double BMI = weight/(height*height);
view_result.setText(getString(R.string.result)+df.format(BMI));
if(BMI>25){
view_suggest.setText(R.string.advice_heavy);
}else if(BMI<25){
view_suggest.setText(R.string.advice_light);
}else{
view_suggest.setText(R.string.advice_average);
}
}
private void setListeners(){
button_back.setOnClickListener(backMain);
}
private Button.OnClickListener backMain = new Button.OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Report.this.finish();
}
};
}
layout/report.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
<TextView android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
<Button android:id="@+id/report_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/report_back"/>
</LinearLayout>
values/report.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="report_title">BMI报告</string>
<string name="report_back">前一页</string>
</resources>
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.
package="com.demo.android.bmi"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Bmi"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Report" android:label="@string/report_title"></activity>
</application>
</manifest>
我找抄书的,死活出现 上面错误,求求帮帮忙