android BMI实例改进

在使用UI组建,使用消息传递对象intent,及偏好设置

在MainActivity中代码:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 
 private Button button_calc;
 private EditText field_height;
 private EditText field_weight;
 
 protected static final int MENU_ABOUT = Menu.FIRST;
 protected static final int MENU_QUIT = Menu.FIRST + 1;
 
 private static final String TAG = "bmi";
 private static final String PREF = "bmi_bref";
 private static final String PREF_HEIGHT = "bmi_height";
 
 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        findViews();
        restorePrefs();
        button_calc.setOnClickListener(calcBMI);
        Log.d(TAG, "set listener");
    }
   
    @Override
    protected void onStop() {
     // TODO Auto-generated method stub
     super.onStop();
     SharedPreferences settings = getSharedPreferences(PREF, 0);
     settings.edit().putString(PREF_HEIGHT, field_height.getText()
       .toString()).commit();
    }
   
    private void restorePrefs() {
     SharedPreferences settings = getSharedPreferences(PREF, 0);
     String pref_height = settings.getString(PREF_HEIGHT, "");
     if (pref_height != "") {
      field_height.setText(pref_height);
      field_weight.requestFocus();
     }
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
     // TODO Auto-generated method stub
     menu.add(0, MENU_ABOUT, 0, "关于");
     menu.add(0, MENU_QUIT, 0, "退出");
     
     return super.onCreateOptionsMenu(menu);
    }
   
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
     switch (item.getItemId()) {
      case MENU_ABOUT:
       openOptionsDialog();
       break;
      case MENU_QUIT:
       finish();
       break;
     }
     return super.onMenuItemSelected(featureId, item);
    }
   
    private void findViews() {
     button_calc = (Button) findViewById(R.id.bmiButton);
     field_height = (EditText) findViewById(R.id.height);
     field_weight = (EditText) findViewById(R.id.weight);
     Log.d(TAG, "find Views");
    }
   
    private OnClickListener calcBMI = new OnClickListener(){

  public void onClick(View v) {
   Intent intent = new Intent(MainActivity.this, ReportActivity.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);
   
  }
  
    };
   
    //创建对话框
    private void openOptionsDialog() {
//   Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_LONG).show();
     new AlertDialog.Builder(MainActivity.this).setTitle(R.string.about_title)
     .setMessage(R.string.about_msg).setPositiveButton(R.string.about_positive,
       new DialogInterface.OnClickListener() {
      
      public void onClick(DialogInterface dialog,
        int which) {
      }
      
     }
     ).show();
    }
   
}

 

在values文件夹中添加一个report.xml

创建内容<string name="report_title">BMI</string>
            <string name="report_back">BMI结果</string>

 

在layout文件夹中页添加一个report.xml,代码是:

<?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"
    >
   
    <Button
     android:id="@+id/report_back"
     android:layout_height="wrap_content"
     android:layout_width="fill_parent"
     android:text="@string/report_back"
     />
   
    <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>

 

在activity包中创建一个ReportActivity.java

代码是:

public class ReportActivity extends Activity {
 private Button button_back;
 private TextView view_result;
 private TextView view_suggest;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.report);
  
  findViews();
  button_back.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    DecimalFormat nf = new DecimalFormat("0.00");
    Bundle bundle = getIntent().getExtras();
    double height = Double.parseDouble(bundle.getString("KEY_HEIGHT")) / 100;
    double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
    double bmi = weight / (height * height);
    view_result.setText("you bmi is: " + nf.format(bmi));
    
    if (bmi > 25) {
     view_suggest.setText(R.string.suggest_heavy);
     
    } else if (bmi < 20) {
     view_suggest.setText(R.string.suggest_light);
    } else {
     view_suggest.setText(R.string.suggest_average);
    }
    
   }
   
  });
 }
 
 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);
 }

}
实现的效果是在运行后,在BMI界面中分别输入升高和体重,点击按钮将,界面将跳转到另一界面,再次点击界面可以看到结果;在点击返回或者home按钮后,可以看到之前的升高输入的值依然在(即SharedPreferences起的作用)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VoidHope

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值