深入浅出Android:初识Intent(BMI)

1、strings.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="app_name">BMI</string>
 5         <string name="height">身高(cm)</string>
 6         <string name="weight">体重(kg)</string>
 7         <string name="bmi_btn">计算BMI值</string>
 8         <string name="bmi_result">你的BMI值是</string>
 9         <string name="about_title">关于Android BMI</string>
10         <string name="about_msg">Android BMI Calc\n
11             作者 lovemu\n\n
12             lovemu+android[at]gmail.com</string>
13         <string name="ok_label">确认</string>
14         <string name="homepage_label">首页</string>
15         <string name="homepage_uri">http://androidbmi.googlecode.com/</string>
16     <string name="hello_world">Hello world!</string>
17     <string name="menu_settings">Settings</string>
18 
19 </resources>

2、MainActivity.java

  1 package example.bmi;
  2 
  3 import java.text.DecimalFormat;
  4 
  5 import android.net.Uri;
  6 import android.os.Bundle;
  7 import android.app.Activity;
  8 import android.app.AlertDialog;
  9 import android.content.DialogInterface;
 10 import android.content.Intent;
 11 import android.view.Menu;
 12 import android.view.View;
 13 import android.view.View.OnClickListener;
 14 import android.widget.Button;
 15 import android.widget.EditText;
 16 import android.widget.TextView;
 17 
 18 public class MainActivity extends Activity {
 19 
 20     @Override
 21     protected void onCreate(Bundle savedInstanceState) {
 22         super.onCreate(savedInstanceState);
 23         setContentView(R.layout.activity_main);
 24         
 25         findViews();
 26         setListensers();
 27     }
 28     private Button button_calc;
 29     private EditText field_height;
 30     private EditText field_weight;
 31     private TextView view_result;
 32     private TextView view_suggest;
 33     private void findViews()
 34     {
 35         button_calc=(Button)findViewById(R.id.submit);
 36         field_height=(EditText)findViewById(R.id.height);
 37         field_weight=(EditText)findViewById(R.id.weight);
 38         view_result=(TextView)findViewById(R.id.result);
 39         view_suggest=(TextView)findViewById(R.id.suggest);
 40     }
 41     private void setListensers()
 42     {
 43         button_calc.setOnClickListener(calcBMI);
 44     }
 45     private Button.OnClickListener calcBMI=new Button.OnClickListener()
 46     {
 47         public void onClick(View v)
 48         {
 49             DecimalFormat nf=new DecimalFormat("0.00");
 50 
 51             double height=Double.parseDouble(field_height.getText().toString())/100;
 52             double weight=Double.parseDouble(field_weight.getText().toString());
 53             double BMI=weight/(height*height);
 54             view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
 55             //give health advice
 56             if(BMI>25)
 57             {
 58                 view_suggest.setText(R.string.advice_heavy);
 59             }
 60             else if(BMI<20)
 61             {
 62                 view_suggest.setText(R.string.advice_light);
 63                }
 64             else
 65             {
 66                 view_suggest.setText(R.string.advice_average);
 67             }
 68             openOptionsDialog();
 69         }    
 70     };
 71     private void openOptionsDialog()
 72     {
 73         new AlertDialog.Builder(MainActivity.this)
 74             .setTitle(R.string.about_title)
 75             .setMessage(R.string.about_msg)
 76             .setPositiveButton(R.string.ok_label,
 77                     new DialogInterface.OnClickListener() {
 78                 public void onClick(
 79                         DialogInterface dialoginterface,int i){
 80                    }
 81                 
 82                 })
 83             .setNegativeButton(R.string.homepage_label,
 84                     new DialogInterface.OnClickListener() {
 85                         
 86                         @Override
 87                         public void onClick(DialogInterface dialog, int which) {
 88                             // TODO Auto-generated method stub
 89                             Uri uri=Uri.parse(getString(R.string.homepage_uri));
 90                             Intent intent=new Intent(Intent.ACTION_VIEW,uri);
 91                             startActivity(intent);
 92                         }
 93                     })
 94             .show();
 95     }
 96 
 97     @Override
 98     public boolean onCreateOptionsMenu(Menu menu) {
 99         // Inflate the menu; this adds items to the action bar if it is present.
100         getMenuInflater().inflate(R.menu.activity_main, menu);
101         return true;
102     }
103     
104 }

3、效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值