标准体重计算器
如果是男性,标准体重=(身高-80)*0.7
如果是女性,标准体重=(身高-70)*0.6
layout中设计两个布局文件:
activity_xiao.xml用来显示初始时的界面,填入信息后跳转到第二个界面显示标准体重
activity_second.xml 第二个界面,显示体重
src中两个activity:
Xiao.java中
package com.example.newx;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.app.Activity;
import android.content.Intent;
public class Xiao extends Activity {
RadioGroup rg=null;
String xingBie="男";//默认值为男
Button button;
EditText editT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xiao);
//找到相应的组件
rg=(RadioGroup)this.findViewById(R.id.group);
button=(Button)this.findViewById(R.id.button1);
editT=(EditText)this.findViewById(R.id.editText1);
//对RadioGroup组件进行监听,从而确定按下的是男还是女
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId)
{
case R.id.nan:
xingBie="男";break;
case R.id.nv:
xingBie="女";break;
}
}
});//radioGroup判断结束
//对计算按钮进行监听,并性别及身高的值传到下一个activity
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String shenGao=editT.getText().toString();//取得身高的值
Intent intent=new Intent();//利用intent进行跳转
intent.setClass(Xiao.this, SecondActivity.class);
Bundle bundle=new Bundle();//利用Bundle进行传值
bundle.putString("xingBie", xingBie);//在Bundle中放入
bundle.putString("shenGao", shenGao);
intent.putExtras(bundle);
startActivity(intent);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.xiao, menu);
return true;
}
}
SecondActivity.java中
setContentView(R.layout.activity_second);
tV=(TextView)this.findViewById(R.id.result);
//b=(Button)this.findViewById(R.id.button1);
Intent intent=getIntent();//获得Intent
Bundle bundle=intent.getExtras();//取得Intent绑定的Bundle
String xingBie=bundle.getString("xingBie");//取得性别和身高信息
shenGao=bundle.getString("shenGao");
//根据性别进行相应的计算,并显示
if(xingBie.equals("男"))
{
double tiZhong=((Integer.parseInt(shenGao))-80)*0.7;
tV.setText("您是一名男性\n身高为:"+shenGao+"\n标准体重为:"+tiZhong);
}
else
{
double tiZhong=((Integer.parseInt(shenGao))-70)*0.6;
tV.setText("您是一名女性\n身高为:"+shenGao+"\n标准体重为:"+tiZhong);
}
}
//第一种finish()
public void fanHui(View view)
{
SecondActivity.this.finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}