实现标准体重计算器的功能
效果图:
main.xml
1 28 17 1827 28 37 38 47 48 56 57 67 73 74 80 81 82 91 92
mylayout.xml
1 26 14 21
strings.xml
1 23 4 Hello World, Test01Activity! 5标准体重计算器 6计算您的标准体重! 7身高: 8性別: 9
Test01Activity.java
1 package xiacl.test; 2 /*import 相关class*/ 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.RadioButton;10 11 12 public class Test01Activity extends Activity {13 private EditText et;14 private RadioButton rb1;15 private RadioButton rb2;16 /** Called when the activity is first created. */17 @Override18 public void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 /*加载main.xml Layout*/21 setContentView(R.layout.main);22 23 /*以findViewById()取得Button对象,并添加onClickListener*/24 Button b1=(Button)findViewById(R.id.button1);25 b1.setOnClickListener(new Button.OnClickListener()26 {27 @Override28 public void onClick(View v){29 /*取得输入的身高*/30 et=(EditText)findViewById(R.id.height);31 double height=Double.parseDouble(et.getText().toString());32 /*取得选择的性别*/33 String sex="";34 rb1=(RadioButton)findViewById(R.id.sex1);35 rb2=(RadioButton)findViewById(R.id.sex2);36 if(rb1.isChecked())37 {38 sex="M";39 }40 else41 {42 sex="F";43 }44 /*new 一个Intent对象,并指定class*/45 Intent intent=new Intent();46 intent.setClass(Test01Activity.this, Test02.class);47 48 /*new一个Bundle对象,并将要传递的数据传入*/49 Bundle bundle=new Bundle();50 bundle.putDouble("height", height);51 bundle.putString("sex", sex);52 53 /*将Bundle对象assign给Intent*/54 intent.putExtras(bundle);55 56 /*调用Activity Test01Activity*/57 /*使用startActivity(intent);就不用返回数据*/58 startActivityForResult(intent,0);59 }60 });61 }62 /*重写OnActivityResult{}*/63 @Override64 protected void onActivityResult(int requestCode,int resultCode,Intent data)65 {66 switch(resultCode)67 {68 case RESULT_OK:69 Bundle bunde=data.getExtras();70 String sex=bunde.getString("sex");71 double height=bunde.getDouble("height");72 73 et.setText(""+height);74 if(sex.equals("M"))75 {76 rb1.setChecked(true);77 }78 else79 {80 rb2.setChecked(true);81 }82 break;83 default:84 break;85 }86 }87 88 }
Test02.java
1 package xiacl.test; 2 /*import 相关class*/ 3 import java.text.DecimalFormat; 4 import java.text.NumberFormat; 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.Button;10 import android.widget.TextView;11 12 13 public class Test02 extends Activity {14 Bundle bundle;15 Intent intent;16 /** Called when the activity is first created. */17 @Override18 public void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 /*加载mylayout.xml Layout*/21 setContentView(R.layout.mylayout);22 23 /*取得Intent中的Bundle对象*/24 intent=this.getIntent();25 bundle=this.getIntent().getExtras();26 /*取得Bundle对象中的数据*/27 String sex=bundle.getString("sex");28 double height=bundle.getDouble("height");29 30 /*判断性别*/31 String sexText="";32 if(sex.equals("M"))33 {34 sexText="男性";35 }36 else37 {38 sexText="女性";39 }40 /*取得标准体重*/41 String weight=this.getWeight(sex, height);42 43 /*设置输出文字*/44 TextView tv1=(TextView)findViewById(R.id.text1);45 tv1.setText("您是一位"+sexText+"\n您的身高是"46 +height+"厘米\n您的标准体重是"+weight+"公斤");47 Button b1=(Button)findViewById(R.id.button1);48 b1.setOnClickListener(new Button.OnClickListener()49 {50 @Override51 public void onClick(View v) {52 // TODO Auto-generated method stub53 /*返回result到上一个Activity*/54 Test02.this.setResult(RESULT_OK,intent);55 /*结束这个Activity,这样也就回到上一个Activity了*/56 Test02.this.finish();57 }58 });59 60 }61 /*四舍五入的method*/62 private String format(double num)63 {64 NumberFormat formatter=new DecimalFormat("0.00");65 String s=formatter.format(num);66 return s;67 }68 /*取得标准体重的method*/69 private String getWeight(String sex,double height)70 {71 String weight="";72 if(sex.equals("M"))73 {74 weight=format((height-80)*0.7);75 }76 else77 {78 weight=format((height-70)*0.6);79 }80 return weight;81 }82 }