cookbook第六章 身高体重计算

这个东西很简单 估计有点了解的都会自己实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/purple"
        android:text="请输入身高   :"
        />

    <EditText
        android:id="@+id/calcute_height"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="5"
        android:phoneNumber="true"
        android:numeric="integer" />
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cm"
        />

    </LinearLayout>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择性别   :"
            />
        <RadioGroup 
            android:id="@+id/radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/radiobutton_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <RadioButton 
             android:id="@+id/radiobutton_woman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
        </RadioGroup>
    </LinearLayout>
    <Button 
        android:id="@+id/calcute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算"
        />
    <TextView 
        android:id="@+id/instruction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

然后是计算:

package com.example.cookbook;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class CalcuteWeight extends Activity{
	
	private EditText calcute_et;
	private RadioButton rb_man;
	private RadioButton rb_women;
	private TextView calcute_height;
	private RadioGroup rg;
	private Button btn_calcute;
	private String sex=null;
	private int height;
	private String instruction;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calcuteweight);
		
		calcute_height=(TextView)findViewById(R.id.instruction);
		calcute_et=(EditText)findViewById(R.id.calcute_height);
		rg=(RadioGroup)findViewById(R.id.radiogroup);
		btn_calcute=(Button)findViewById(R.id.calcute);
		height=calcute_et.getInputType();
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(RadioGroup arg0,int arg1)
			{
				int rbId=arg0.getCheckedRadioButtonId();
				RadioButton rb=(RadioButton)CalcuteWeight.this.findViewById(rbId);
				sex=(String) rb.getText();
			}
		});
		btn_calcute.setOnClickListener(new OnClickListener(){
			public void onClick(View v)
			{
				try
				{
					height=Integer.parseInt(calcute_et.getText().toString());
					if(height<100)
					{
						Toast.makeText(getApplicationContext(), "请输入成年人的身高", Toast.LENGTH_SHORT).show();
					}
					else if(height>230)
					{
						Toast.makeText(getApplicationContext(), "请输入正常人的身高", Toast.LENGTH_SHORT).show();
					}
					else{
				    if(sex.equals("男"))
				      {
				      instruction="您性别为:"+sex+"性,"+"您的标准体重应该为:"+(height-100)*0.9+"kg";
			        	}
			    else if(sex.equals("女"))
				     {
				     instruction="您性别为:"+sex+"性,"+"您的标准体重应该为"+((height-100)*0.9-2.5)+"kg";
				      }
				else	if(sex==null)
					{
						Toast.makeText(getApplicationContext(), "请选择性别", Toast.LENGTH_SHORT).show();
					}
				calcute_height.setText(instruction);
				}
				}
				catch(Exception e)
				{
					Toast.makeText(getApplicationContext(), "请正确输入数据", Toast.LENGTH_SHORT).show();
					e.printStackTrace();}
				}
		});
		
	}

}
OK 全部完工;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值