标准体重计算器
一、运行效果
二、核芯代码
MainActivity.java
package cn.edu.bzu.tizhongji;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
public class MainActivity extends Activity implements OnClickListener {
protected int my_requestCode=1550;
private RadioButton radio1,radio2;
private EditText edit_height;
public void onCreate(Bundle savedlnstanceState){
super.onCreate(savedlnstanceState);
Button ok=(Button)findViewById(R.id.button1);
ok.findViewById(R.id.button1).setOnClickListener((android.view.View.OnClickListener) this);
radio1=(RadioButton) findViewById(R.id.radio1);
radio2=(RadioButton) findViewById(R.id.radio2);
edit_height=(EditText) findViewById(R.id.editText1);
}
public void onClick(View v){
int id=v.getId();
if(id==R.id.button1){
double height=Double.parseDouble(edit_height.getText().toString());
String sex="";
RadioButton rb1=(RadioButton)findViewById(R.id.radio1);
if(rb1.isChecked()){
sex="男";
}else{
sex="女";
}
Intent intent=new Intent();
intent.setClass(MainActivity.this, ShowActivity.class);
Bundle bundle=new Bundle();
bundle.putDouble("height", height);
bundle.putString("sex", sex);
intent.putExtras(bundle);
startActivityForResult(intent, my_requestCode);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_OK:
Bundle bunde=data.getExtras();
String sex=bunde.getString("sex");
double height=bunde.getDouble("height");
edit_height.setText("" + height);
if(sex.equals("M")){
radio1.setChecked(true);
}else{
radio2.setChecked(false);
}break;
default:break;
}
}
@Override
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;
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
ShowActivity.java
<p>package cn.edu.bzu.tizhongji;</p><p>import java.text.DecimalFormat;
import java.text.NumberFormat;</p><p>import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;</p><p>public class ShowActivity extends Activity implements OnClickListener{
private Intent intent;
private Bundle bundle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button_back);
findViewById(R.id.button_back).setOnClickListener(this);
bundle=this.getIntent().getExtras();
String sex=bundle.getString("sex");
double height=bundle.getDouble("height");
String sexText="";
if(sex.equals("男")){
sexText="男性";
}else {
sexText="女性";
}
String weight=this.getWeight(sex,height);
TextView tv1=(TextView)findViewById(R.id.text1);
tv1.setText("你是一位" + sexText + "\n你的身高是" + height +"厘米\n你的标准体重是" + weight + "公斤");
}
private String format(double num){
NumberFormat formatter=new DecimalFormat("0.00");
String s=formatter.format(num);
return s; }
private String getWeight(String sex, double height) {
String weight="";
if(sex.equals("男")){
weight=format((height-80)*0.7);
}else{
weight=format((height-70)*0.6);
}
return weight; }
public void onClick(View v) {
int id=v.getId();
if(id==R.id.button_back){
ShowActivity.this.setResult(RESULT_OK, intent);
ShowActivity.this.finish();
}
}
}
</p>
三、总结
对于这个任务,对初学者来说还挺难的,仔细观看老师的视频数遍,才做出来,但是很遗憾的是,没能运行成功,不知道为什么 ?