<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" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/top_text" /> <RadioGroup android:id="@+id/gadiogroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginLeft="80dp" android:layout_marginTop="30dp" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioButton1_text" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radioButton2_text" /> </RadioGroup> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/gadiogroup" android:layout_marginBottom="14dp" android:layout_marginRight="27dp" android:layout_toLeftOf="@+id/textView1" android:text="@string/sex_text" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/gadiogroup" android:layout_marginBottom="-60dp" android:layout_marginRight="27dp" android:layout_toLeftOf="@+id/textView1" android:text="@string/shenggao_text" /> <EditText android:id="@+id/edittext" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_alignTop="@+id/textView3" android:ems="10" android:inputType="number" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext" android:layout_marginTop="32dp" android:layout_toRightOf="@+id/textView3" android:text="@string/button_text" /> </RelativeLayout>
activity_activity02.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" tools:context=".Activity02" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="16dp"/> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_below="@+id/textView2" android:layout_marginTop="14dp"/> </RelativeLayout>
MainActivity.java我们在设计中添加了RadioButton控件,并将它放在了RadioGroup组中,这样来做选择男、女的性别比较简单、方便。但是我们知道RadioButton只能选中一个,所以,在MainActivity.java中就需要添加RadioButton的监听。
private RadioGroup rg; private Button button; static String sex;rg = (RadioGroup)this.findViewById(R.id.gadiogroup); button = (Button)this.findViewById(R.id.button); rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radioButton1: sex = "F";break; case R.id.radioButton2: sex = "M";break; } } });添加完RadioButton的监听后,就需要添加按钮Button的click监听事件,在其中要把RadioButton的监听事件包含进去,并且还要向Activity02中传递数据。传递数据的时候就要用到bundle.putString(String key, String value)方法。
首先我来介绍一下传递数据的过程:
Bundle bundle = new Bundle(); bundle.putString("sex", "男性"); Intent intent = new Intent(); intent.putExtras(bundle); intent.setClass(MainActivity.this, Activity02.class); startActivity(intent);
在另一个Activity02中接受数据:Intent intent = getIntent(); Bundle bundle=intent.getExtras(); String sex=bundle.getString("sex"); Toast.makeText(this, "您选择的是:"+sex, Toast.LENGTH_LONG);这只是简单的数据传递过程,那么现在我们就把上边这个“计算标准体重”的案例的Button的click事件补充完整:
public class MainActivity extends Activity {private RadioGroup rg; private Button button; private EditText et; static String sex="F" ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg = (RadioGroup)this.findViewById(R.id.gadiogroup); button = (Button)this.findViewById(R.id.button); et = (EditText)this.findViewById(R.id.edittext); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Bundle bundle = new Bundle(); rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch(checkedId){ case R.id.radioButton1: sex = "F";break; case R.id.radioButton2: sex = "M";break; } } }); int a =Integer.parseInt(et.getText().toString()) ; if(sex == "F"){ String b = (a-80)*0.70+""; bundle.putString("sex", "男性"); bundle.putString("result", b); }else if(sex == "M"){ String c = (a-70)*0.60+""; bundle.putString("sex", "女性"); bundle.putString("result", c); } bundle.putString("shengao", et.getText().toString()); Intent intent = new Intent(); intent.putExtras(bundle); intent.setClass(MainActivity.this, Activity02.class); startActivity(intent); } }); }}Activity02.java
public class Activity02 extends Activity { private TextView text1,text2,text3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity02); text1 = (TextView)this.findViewById(R.id.textView1); text2 = (TextView)this.findViewById(R.id.textView2); text3 = (TextView)this.findViewById(R.id.textView3); Intent intent = getIntent(); Bundle bundle=intent.getExtras(); String sex=bundle.getString("sex"); text1.setText("您选择的是"+sex); String shengao=bundle.getString("shengao"); text2.setText("您的身高是"+shengao+"厘米"); String result=bundle.getString("result"); text3.setText("您的标准体重是:"+result+"公斤"); } }
这样,你就可以做一个Android应用小程序-----《计算你的标准体重》,还等什么,赶快行动吧。