标准体重计算器

第一个页面

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;
 }
}
  
 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值