学习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

更多相关文章

  1. android布局居中
  2. Android布局LinearLayout子控件无法填充问题
  3. 如果Imageview与Linearlayout有叠加且可选资源长度不同,如何布局?
  4. 2013.11.29 ——— android layout不显示布局绘图 显示空白
  5. Android用户界面与布局
  6. 安卓XML布局,相对布局的常用属性~
  7. Android 布局学习利器-Hierarchy Viewer
  8. Android如何实现模态对话框(Modal Dialog)
  9. 相对布局常用属性介绍(RelativeLayout)

随机推荐

  1. android 网络框架性能优化分析
  2. android 手把手教你打造属于自己的文件浏
  3. Android变形矩阵——Matrix
  4. android 内存优化详解
  5. 一个轻量级、可插拔的Android消息推送框
  6. Android调用系统相册和相机选择图片并显
  7. 如何在Android上快速集成微信和QQ的分享(S
  8. 关于android双卡手机sim卡信息采集适配的
  9. Android版本适配(基于 6.0 ~ 9.0)
  10. Android有毒,三星或将打造独立生态圈