实现标准体重计算器的功能
效果图:
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <AbsoluteLayout 3 android:id="@+id/widget0" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 xmlns:android="http://schemas.android.com/apk/res/android" 7 > 8 <TextView 9 android:id="@+id/title" 10 android:layout_width="243dp" 11 android:layout_height="29dp" 12 android:text="@string/title" 13 android:textSize="24sp" 14 android:layout_x="36dp" 15 android:layout_y="32dp" 16 > 17 </TextView> 18 <TextView 19 android:id="@+id/text1" 20 android:layout_width="wrap_content" 21 android:layout_height="37dp" 22 android:text="@string/text1" 23 android:textSize="18sp" 24 android:layout_x="40dp" 25 android:layout_y="156dp" 26 > 27 </TextView> 28 <TextView 29 android:id="@+id/text2" 30 android:layout_width="wrap_content" 31 android:layout_height="29dp" 32 android:text="@string/text2" 33 android:textSize="18sp" 34 android:layout_x="40dp" 35 android:layout_y="102dp" 36 > 37 </TextView> 38 <TextView 39 android:id="@+id/text3" 40 android:layout_width="wrap_content" 41 android:layout_height="wrap_content" 42 android:text="cm" 43 android:textSize="18sp" 44 android:layout_x="231dp" 45 android:layout_y="157dp" 46 > 47 </TextView> 48 <Button 49 android:id="@+id/button1" 50 android:layout_width="70dp" 51 android:layout_height="48dp" 52 android:text="计算" 53 android:layout_x="130dp" 54 android:layout_y="232dp" 55 > 56 </Button> 57 <RadioGroup 58 android:id="@+id/sex" 59 android:layout_width="300dp" 60 android:layout_height="100dp" 61 xmlns:android="http://schemas.android.com/apk/res/android" 62 android:layout_x="97dp" 63 android:layout_y="98dp" 64 android:orientation="horizontal" 65 android:checkedButton="@+id/sex1" 66 > 67 <RadioButton 68 android:id="@+id/sex1" 69 android:layout_width="wrap_content" 70 android:layout_height="wrap_content" 71 android:text="男" 72 > 73 </RadioButton> 74 <RadioButton 75 android:id="@+id/sex2" 76 android:layout_width="wrap_content" 77 android:layout_height="wrap_content" 78 android:text="女" 79 > 80 </RadioButton> 81 </RadioGroup> 82 <EditText 83 android:id="@+id/height" 84 android:layout_width="130dp" 85 android:layout_height="wrap_content" 86 android:textSize="18sp" 87 android:layout_x="96dp" 88 android:layout_y="142dp" 89 android:numeric="decimal" 90 > 91 </EditText> 92 </AbsoluteLayout>
mylayout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 > 6 <TextView 7 android:id="@+id/text1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:textSize="20sp" 11 android:layout_x="50dp" 12 android:layout_y="72dp" 13 /> 14 <Button 15 android:id="@+id/button1" 16 android:layout_width="100dp" 17 android:layout_height="48dp" 18 android:text="回到上一页" 19 android:layout_x="100dp" 20 android:layout_y="180dp"/> 21 </AbsoluteLayout>
strings.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="hello">Hello World, Test01Activity!</string> 5 <string name="app_name">标准体重计算器</string> 6 <string name="title">计算您的标准体重!</string> 7 <string name="text1">身高:</string> 8 <string name="text2">性別:</string> 9 </resources>
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 @Override 18 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 @Override 28 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 else 41 { 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 @Override 64 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 else 79 { 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 @Override 18 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 else 37 { 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 @Override 51 public void onClick(View v) { 52 // TODO Auto-generated method stub 53 /*返回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 else 77 { 78 weight=format((height-70)*0.6); 79 } 80 return weight; 81 } 82 }