深入浅出Android:初识Intent(BMI)
2012-11-29 23:11
316 查看
1、strings.xml
2、MainActivity.java
3、效果
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">BMI</string> <string name="height">身高(cm)</string> <string name="weight">体重(kg)</string> <string name="bmi_btn">计算BMI值</string> <string name="bmi_result">你的BMI值是</string> <string name="about_title">关于Android BMI</string> <string name="about_msg">Android BMI Calc\n 作者 lovemu\n\n lovemu+android[at]gmail.com</string> <string name="ok_label">确认</string> <string name="homepage_label">首页</string> <string name="homepage_uri">http://androidbmi.googlecode.com/</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> </resources>
2、MainActivity.java
package example.bmi; import java.text.DecimalFormat; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViews(); setListensers(); } private Button button_calc; private EditText field_height; private EditText field_weight; private TextView view_result; private TextView view_suggest; private 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); } private void setListensers() { button_calc.setOnClickListener(calcBMI); } private Button.OnClickListener calcBMI=new Button.OnClickListener() { public void onClick(View v) { DecimalFormat nf=new DecimalFormat("0.00"); double height=Double.parseDouble(field_height.getText().toString())/100; double weight=Double.parseDouble(field_weight.getText().toString()); double BMI=weight/(height*height); view_result.setText(getText(R.string.bmi_result)+nf.format(BMI)); //give health advice 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(); } }; private void openOptionsDialog() { new AlertDialog.Builder(MainActivity.this) .setTitle(R.string.about_title) .setMessage(R.string.about_msg) .setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() { public void onClick( DialogInterface dialoginterface,int i){ } }) .setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Uri uri=Uri.parse(getString(R.string.homepage_uri)); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent); } }) .show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
3、效果
相关文章推荐
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL
- Android初识-Intent的用法
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL
- Android深入浅出系列之实例应用—开机就运行自定义的程序BroadcastReceiver和intent-filter的用法
- 深入浅出Android:添加菜单(BMI)
- Android初识-Intent用法进阶篇
- 深入浅出Android安全(1)- Intent Spoofing
- 第69章、初识Intent-打开另一个Activity:单向传值(从零开始学Android)
- 第70章、初识Intent-打开另一个Activity:双向传值(从零开始学Android)
- Android深入浅出系列之实例应用—开机就运行自定义的程序BroadcastReceiver和intent-filter的用法
- 新纪元——初识android(Intent)
- 初识Android之Activity和Intent
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL
- 深入浅出Android:增加对话框功能(BMI)
- 深入浅出Android:加入新的Activity(BMI)
- Android 初识Intent
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL【转】
- 深入浅出Android:纪念人生的第一个android程序(BMI)
- 深入浅出Android:传送数据到新的Activity(BMI)
- 深入浅出 - Android系统移植与平台开发(七)- 初识HAL