MainActivity
package com.example.android_text3; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class MainActivity extends Activity { EditText et; Button bt; RadioButton rb1; RadioButton rb2; String sex=""; Double height=0.0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et=(EditText)findViewById(R.id.editText1); bt=(Button)findViewById(R.id.go); rb1=(RadioButton)findViewById(R.id.radio0); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(rb1.isChecked()){ sex="m"; }else { sex="w"; } String textview=et.getText().toString(); if (textview==null||textview.equals("")) { Toast.makeText(MainActivity.this, "请输入身高", Toast.LENGTH_LONG).show(); } else { height=Double.valueOf(textview); Log.i("height",String.valueOf(height)); Intent intent=new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); Bundle extras=new Bundle(); extras.putString("sex", sex); extras.putDouble("height", height);; intent.putExtras(extras); startActivityForResult(intent, 0); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub switch (resultCode) { case RESULT_OK: Bundle bundle=data.getExtras(); Double weight=bundle.getDouble("weight"); Toast.makeText(MainActivity.this, "体重为"+String.valueOf(weight), Toast.LENGTH_LONG).show(); break; default: break; } } }
SecondActivity.java
package com.example.android_text3; import java.text.DecimalFormat; import java.text.NumberFormat; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends Activity { TextView sex; TextView height; Button bt; Double weight; Intent intent; Bundle bundle; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.secondlayout); sex = (TextView) findViewById(R.id.sex); String sexmessage = this.getIntent().getExtras().getString("sex"); height = (TextView) findViewById(R.id.height); sex.setText("性别:" + getSex(sexmessage)); height.setText("身高:"+formatnum(this.getIntent().getExtras() .getDouble("height"))); bt = (Button) findViewById(R.id.goback); weight = getWeight(sexmessage, this.getIntent().getExtras().getDouble("height")); intent = this.getIntent(); bundle =intent.getExtras(); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub bundle.putDouble("weight", weight); intent.putExtras(bundle); SecondActivity.this.setResult(RESULT_OK, intent); SecondActivity.this.finish(); } }); } /** * * @param values * @return */ String formatnum(double values) { NumberFormat formatter = new DecimalFormat("0.00"); String s = formatter.format(values); return s; } /** * * @param sex * @return */ String getSex(String sex) { if (sex.equals("w")) { sex = "女"; } else { sex = "男"; } return sex; } Double getWeight(String sex, Double height) { Double weight=0.0; if(sex.equals("m")){ weight=(height-80)*0.7; }else{ weight=(height-70)*0.8; } return weight; } }
xml文件
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup1" android:layout_marginTop="42dp" android:layout_toLeftOf="@+id/radioGroup1" android:text="身高:" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_centerHorizontal="true" android:ems="10" android:inputType="number" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/radioGroup1" android:layout_alignLeft="@+id/textView2" android:text="性别:" /> <Button android:id="@+id/go" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="计算" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/height" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/goback" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> </LinearLayout>