现在才接触android,在看了一个案例之后照着携了一个小例子:

第一个页面MainActivity:

package com.hoperun.activity;

import java.text.DecimalFormat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
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;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;

protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViews();
button_calc.setOnClickListener(calcBMI);

}

@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);
view_result = (TextView) findViewById(R.id.result);
view_suggest = (TextView) findViewById(R.id.suggest);
}

private OnClickListener calcBMI = new OnClickListener(){

private AlertDialog show;

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("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);
}
//openOptionsDialog();
}

};

//创建对话框
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();
}

}

写完后在AndroidManifest.xml中进行注册。


string.xml中的代码为:

<?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="suggest_heavy">太重了</string>
<string name="suggest_average">平均</string>
<string name="suggest_light">太轻了</string>
<string name="about_title">关于android bmi</string>
<string name="about_msg">android bmi calculation</string>
<string name="about_positive">确定</string>
</resources>


在界面描述文件中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/height"
/>
<EditText
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/weight"
/>
<EditText
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>

<Button
android:id="@+id/bmiButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算BMI值"
/>
<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>

主要的内容在这里就算是完成了,下来就可以运行android project了


更多相关文章

  1. Android源代码编译
  2. Android菜鸟日记14-对话框
  3. Android应用程序组件Content Provider的启动过程源代码分析(6)
  4. android常用代码片段
  5. Android系统进程Zygote启动过程的源代码分析(3)
  6. Android webview注入自己的js代码(js传入function等其他参数解决)
  7. Android屏幕手势检测的实现代码
  8. 【Android 内存优化】Bitmap 硬盘缓存 ( Google 官方 Bitmap 示
  9. Android一套代码适配不同Android版本终极指南

随机推荐

  1. Android(安卓)Mms专题之:Android短彩信收
  2. Android(安卓)ViewPager轮播图
  3. APIcloud 按两次返回键退出程序,android
  4. Android(安卓)Audio代码分析6 - AudioEff
  5. Android(安卓)入门系列文章
  6. 浅谈Android异步任务
  7. 《第一行代码》学习笔记 第 3 章
  8. Android(安卓)Studio 如何快速添加overri
  9. 自定义Activity间跳转效果
  10. Android(安卓)studio虚拟机adb环境配置