Android_RadioButton,CheckBox
xml文件:
<LinearLayout 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:orientation="vertical" tools:context="com.example.day03.MainActivity" > <!-- RadioGroup 为单选框分组 --> <RadioGroup android:id="@+id/group_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked = "true" android:text="男" /> <RadioButton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <Button android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:onClick="click"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学习的课程:" /> <CheckBox android:id="@+id/language" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="语文"/> <CheckBox android:id="@+id/math" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数学"/> <CheckBox android:id="@+id/english" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="英语"/> <Button android:id="@+id/buttton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="commit" android:onClick="click1"/> </LinearLayout>
源代码:
package com.example.day03; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { RadioGroup radioGroup; CheckBox[] checks; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = (RadioGroup) findViewById(R.id.group_sex); checks = new CheckBox[3]; //找出对应的控件 checks[0] = (CheckBox) findViewById(R.id.language); checks[1] = (CheckBox) findViewById(R.id.math); checks[2] = (CheckBox) findViewById(R.id.english); } public void click(View v){ //找出RadioGroup选中的radioButton //方法一:通过RadioGroup的getCheckedRAdioButtonId()方法找到被选中的id int id = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) findViewById(id); Toast.makeText(MainActivity.this, radioButton.getText().toString(), Toast.LENGTH_SHORT).show(); //方法二:遍历RadioGroup下面所有的RadioButton,找出选中的项 for (int i = 0; i < radioGroup.getChildCount(); i++) { RadioButton radioButton1 = (RadioButton) radioGroup.getChildAt(i); if(radioButton1.isChecked()){ Toast.makeText(MainActivity.this, radioButton1.getText().toString(), Toast.LENGTH_SHORT).show(); } } } //点击该按钮时调用该方法 public void click1(View v){ String result = ""; //遍历checks数组找出选中项 for (CheckBox check : checks) { if(check.isChecked()){ result += check.getText().toString(); } } Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } }