android 二 BMI 小例子

学习android已经一段时间了,今天结合android的一些基础,做了一个关于BMI的例子, 包括的事件的触发,菜单和对话框的建立,还有android的布局。

 

main.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"
    >
   
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/us_height"
    />
   
    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/feet"
    />

    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/inch"
    />
   
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/us_weight"
    />
   
    <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/weight"
     android:inputType="numberDecimal"
    />
   
    <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="@string/bmi_btn"
     android:id="@+id/btn_callBmi"
    />
   
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/result"
     android:singleLine="true"
    />
   
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/suggest"
    />
</LinearLayout>

 

 

Abmi.java

 

package com.roy.android.abmi;

import java.text.DecimalFormat;
import java.util.logging.Logger;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
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 Abmi extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findView();
        setListeners();
    }
   
    private static final String TAG = "Abmi";
    private Button btn_call;
    private EditText field_feet;
    private EditText field_inch;
    private EditText field_weight;
    private TextView view_suggest;
    private TextView view_result;

    private void findView(){
     Log.d(TAG, "findView");
     btn_call = (Button) this.findViewById(R.id.btn_callBmi);
     field_feet = (EditText) this.findViewById(R.id.feet);
     field_inch = (EditText) this.findViewById(R.id.inch);
     field_weight = (EditText) this.findViewById(R.id.weight);
     view_suggest = (TextView) this.findViewById(R.id.suggest);
     view_result = (TextView) this.findViewById(R.id.result);
    }
 
    private void setListeners(){
     Log.d(TAG, "setListeners");
     btn_call.setOnClickListener(callBMI);
    }
   
   private Button.OnClickListener callBMI = new Button.OnClickListener() {
  
  @Override
  public void onClick(View arg0) {
   
   DecimalFormat df = new DecimalFormat("0.00");
   try{
    double height = (Double.parseDouble(field_feet.getText().toString()) * 12
    + Double.parseDouble(field_inch.getText().toString()))* 2.54 / 100;
    double weight = Double.parseDouble(field_weight.getText().toString()) * 0.45359;
    double BMI =  weight/(height*height);
    view_result.setText(getText(R.string.bmi_result) + df.format(BMI));
    Log.d(TAG, df.format(height));
    Log.d(TAG, df.format(weight));
    Log.d(TAG, df.format(BMI));
    if (BMI > 27){
     view_suggest.setText(getText(R.string.advice_fat));
    } else if (BMI > 25){
     view_suggest.setText(getText(R.string.advice_heavy));
    } else if (BMI < 20){
     view_suggest.setText(getText(R.string.advice_light));
    } else {
     view_suggest.setText(getText(R.string.advice_average));
    }
   }catch (Exception e) {
    Toast.makeText(Abmi.this, getText(R.string.input_error),
       Toast.LENGTH_SHORT).show();
   }
  }
 };
   
 protected static final int MENU_ABOUT = Menu.FIRST;
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  super.onCreateOptionsMenu(menu);
  Log.d(TAG, "open_menu");
  menu.add(0, MENU_ABOUT, 0, R.string.about);
  return true;
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  super.onOptionsItemSelected(item);
  Log.d(TAG, "select item option");
  switch(item.getItemId()){
   case MENU_ABOUT:
    openDialog();
    break;
  }
  return true;
 }
 
 private void openDialog(){
  Log.d(TAG, "Open Dailog");
  new AlertDialog.Builder(this)
   .setTitle(R.string.about_title)
   .setMessage(R.string.about_msg)
   .setPositiveButton(R.string.ok_label,
     new DialogInterface.OnClickListener() {
      
      @Override
      public void onClick(DialogInterface arg0, int arg1) {
      }
     }
   ).show();
 }
 
}s

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值