主程序:

package com.leansmall.heightcalculator;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.TextView;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioButton;

import android.view.View.OnClickListener;

public class MainActivity extends Activity {

private Button calculatorButton;

private EditText weightEditText;

private RadioButton manRadioButton;

private RadioButton womanRadioButton;

private TextView resultTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

calculatorButton=(Button)findViewById(R.id.btGetResult);

weightEditText=(EditText)findViewById(R.id.etWeight);

manRadioButton=(RadioButton)findViewById(R.id.rbMan);

womanRadioButton=(RadioButton)findViewById(R.id.rbWoman);

resultTextView=(TextView)findViewById(R.id.tvResult);

OnStart();

}

protected void OnStart() {

super.onStart();

registerEvent();

}

private void registerEvent() {

calculatorButton.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

if(!weightEditText.getText().toString().trim().equals(""))

{

if(manRadioButton.isChecked()||womanRadioButton.isChecked())

{

Double

weight=Double.parseDouble(weightEditText.getText().toString());

StringBuffer sb=new StringBuffer();

sb.append("------评估结果------\n");

if(manRadioButton.isChecked())

{

sb.append("男性标准身高:");

double result=evaluateHeight(weight,"男");

sb.append((int)result+"厘米");

}

if(womanRadioButton.isChecked())

{

sb.append("女性标准身高:");

double result=evaluateHeight(weight,"女");

sb.append((int)result+"厘米");

}

resultTextView.setText(sb.toString());

}

else

{

showMessage("请选择性别");

}

}

else

{

showMessage("请输入体重");

}

}

});

}

private double

evaluateHeight(double weight,String sex)

{

double height;

if(sex=="男")

{

height=170-(62-weight)/0.6;

}

else

{

height=158-(52-weight)/0.5;

}

return height;

}

private void

showMessage(String message)

{

AlertDialog alert=new

AlertDialog.Builder(this).create();

alert.setTitle("系统消息");

alert.setMessage(message);

alert.setButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

}

});

alert.show();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it

is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar

will

// automatically handle clicks on the Home/Up button, so

long

// as you specify a parent activity in

AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

mail.xml文件:

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginTop="20.0dip"

android:background="@layout/activity_main"

android:gravity="center_horizontal"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="3.10" >

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请输入体重(Kg):" />

android:id="@+id/etWeight"

android:layout_width="188dp"

android:inputType="number"

android:layout_height="wrap_content"

android:ems="10"

android:gravity="center" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="3.10"

android:orientation="vertical" >

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="请选择性别:" />

android:id="@+id/radioGroup1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="3.10" >

android:id="@+id/rbMan"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="男" />

android:id="@+id/rbWoman"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="女" />

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_weight="3.10"

android:gravity="center" >

android:id="@+id/btGetResult"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="评测" />

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:gravity="center_horizontal" >

android:id="@+id/tvResult"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="10.0dip"/>

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐