第一个页面
public class MainActivity extends Activity {
Button m_Button;
EditText m_Edit;
RadioGroup m_RadioGroup;
private double height;
private String sex;
@Override
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialComponents();
m_Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m_Edit=(EditText)findViewById(R.id.height);
m_RadioGroup=(RadioGroup)findViewById(R.id.RadioGroup);
height=Double.parseDouble(m_Edit.getText().toString());
if(m_RadioGroup.getCheckedRadioButtonId()==R.id.M)
{
sex="M";
}
else
{
sex="F";
}
Intent intent=new Intent();
intent.setClass(MainActivity.this, ResoutActivity.class);
Bundle bun=new Bundle();
bun.putDouble("height", height);
bun.putString("sex", sex);
intent.putExtras(bun);
startActivity(intent);
//finish();
}
});
}
private void initialComponents() {
// TODO Auto-generated method stub
m_Button=(Button) this.findViewById(R.id.confirm);
m_Edit=(EditText) findViewById(R.id.height);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第二个页面
public class ResoutActivity extends Activity {
private TextView m_TextView1;
private Button m_Button;
private String SexText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_resout);
m_TextView1=(TextView)findViewById(R.id.textView1);
Intent intent=getIntent();
// Bundle bun=this.getIntent().getExtras();
Bundle bun=intent.getExtras();
String Sex=bun.getString("sex");
// Sex=bun.getString("sex");
double Height=bun.getDouble("height");
if(Sex.equals("M"))
{
SexText="男性";
}
else
{
SexText="女性";
}
//取得体重
double Weight=getWeight(Sex, Height);
String result1 = "您是一位" + SexText +'\n'+"您的身高为:"+ Height + "cm"+'\n'+"您的标准体重为:"+ Weight +"千克";
m_TextView1.setTextSize(20);
m_TextView1.setText(result1);
m_Button=(Button)findViewById(R.id.Button1);
m_Button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1=new Intent();
intent1.setClass(ResoutActivity.this, MainActivity.class);
startActivity(intent1);
finish();
}
});
}
//计算体重
public double getWeight(String Sex,double height)
{
double Weight=0;
if(Sex.equals("M"))
{
Weight=(height-80)*0.7;
}
else
{
Weight=(height-70)*0.6;
}
return Weight;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.resout, menu);
return true;
}
}