Android开发:标准身高计算器应用的…

主程序:

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;
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() {
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() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
    alert.show();
   
   
   
    }
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;
}

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"/>

   




转载于:https://my.oschina.net/u/1585312/blog/997463

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值