现在才接触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了