Activity-main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/a"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="51dp"
android:layout_marginTop="78dp"
android:text="@string/str"
android:textSize="22sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginLeft="51dp"
android:layout_marginTop="33dp"
android:layout_toRightOf="@+id/radioGroup1"
android:text="性别:" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginLeft="90dp"
android:layout_marginTop="33dp"
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/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="51dp"
android:layout_marginTop="33dp"
android:text="身高:" />
<EditText
android:id="@+id/heightTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180dp"
android:height="20dp"
android:layout_below="@+id/radioGroup1"
android:layout_marginLeft="90dp"
android:layout_marginTop="33dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/heightTest"
android:layout_marginLeft="90dp"
android:layout_marginTop="33dp"
android:onClick="back"
android:text="计算" />
</RelativeLayout>
Activity-show.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/a"
tools:context=".ShowActivity" >
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview"
android:layout_marginLeft="21dp"
android:layout_marginTop="167dp"
android:layout_toRightOf="@+id/textview"
android:onClick="back"
android:text="@string/back" />
</RelativeLayout>
Main-Activity.java
package bzu.gezi.com.example.android02;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button;
private EditText heightText;
private RadioGroup rg;
private RadioButton rb0,rb1;
private String sex="b";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==rb1.getId()){
sex="g";
}
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String height=heightText.getText().toString();
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("height",height);
bundle.putString("sex",sex);
intent.putExtras(bundle);
intent.setClass(MainActivity.this,ShowActivity.class);
startActivity(intent);
}
});
}
private void init(){
button=(Button) this.findViewById(R.id.button1);
heightText=(EditText) this.findViewById(R.id.heightTest);
rg=(RadioGroup) this.findViewById(R.id.radioGroup1);
rb0=(RadioButton) this.findViewById(R.id.radio0);
rb1=(RadioButton) this.findViewById(R.id.radio1);
}
@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;
}
}
Show-Activity.java
package bzu.gezi.com.example.android02; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ShowActivity extends Activity { private TextView textview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); init(); Bundle bundle=new Bundle(); bundle=getIntent().getExtras(); String height=bundle.getString("height"); String sex=bundle.getString("sex"); int hei=Integer.parseInt(height); float weight; if(sex.equals("b")){ weight=(float) ((hei-80)*0.7); textview.setText("你是一个男性\n你的身高是"+height+"厘米\n你的体重是"+weight+"公斤"); }else{ weight=(float) ((hei-70)*0.6); textview.setText("你是一个女性\n你的身高是"+height+"厘米\n你的体重是"+weight+"公斤"); } } public void back(View view){ finish(); } private void init() { textview=(TextView) this.findViewById(R.id.textview); } /*public void DisplayToast(String str){ Toast.makeText(ShowActivity.this,str,Toast.LENGTH_LONG).show(); }*/ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.show, menu); return true; } }