Activity
res/layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <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:numeric="integer" 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:numeric="integer" android:text="" /> <Button android:id="@+id/submit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/bmi_btn" /> <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="" /> </LinearLayout>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">哈罗, Bmi!</string> <string name="app_name">BMI_App</string> <string name="height">身高(cm)</string> <string name="weight">体重(Kg)</string> <string name="bmi_btn">计算BMI</string> <string name="bmi_result">BMI_App</string> <string name="about_title">关于Android BMI</string> <string name="about_msg">Android BMI Calc\n 作者xxx\n\n gasolin+android [at] gmail.com</string> <string name="ok">确认</string> </resources>
src
package com.demo.bmi; import java.text.DecimalFormat; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; 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 { private Button button; private EditText fieldHeight; private EditText fieldWeight; private TextView result; private TextView suggest; //new一个监听 // private OnClickListener clickListener = new OnClickListener() private Button.OnClickListener clickListener = new Button.OnClickListener(){ @Override public void onClick(View v) { try { double height = Double.parseDouble(fieldHeight.getText().toString())/100; double weight = Double.parseDouble(fieldWeight.getText().toString()); double bmi = weight/(height*height); DecimalFormat df = new DecimalFormat("0.00"); result.setText("你的BMI是" + df.format(bmi)); if(bmi>25){ suggest.setText(R.string.advice_heavy); }else if(bmi<20){ suggest.setText(R.string.advice_light); }else{ suggest.setText(R.string.advice_avg); } } catch (NumberFormatException e) { Toast.makeText(Bmi.this, "请输入数字", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } //执行对话 openOptionsDialog(); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fineView(); setLintener(); } //初始化定位控件 private void fineView(){ button = (Button) findViewById(R.id.submit); fieldHeight = (EditText) findViewById(R.id.height); fieldWeight = (EditText) findViewById(R.id.weight); result = (TextView) findViewById(R.id.result); suggest = (TextView) findViewById(R.id.suggest); } //设置监听 private void setLintener(){ button.setOnClickListener(clickListener); } //执行对话 private void openOptionsDialog(){ /* new AlertDialog.Builder(Bmi.this) .setTitle(R.string.about_title) .setMessage(R.string.about_msg) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){ public void onClick( DialogInterface dialoginterface, int i){ } }) .show(); */ Toast.makeText(Bmi.this, "BMI 计算器", Toast.LENGTH_SHORT).show(); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28