checkbox單選多選

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="性別"/>
    <!--定義一組單選按鈕-->
    <RadioGroup
      android:id="@+id/rg"
      android:orientation="horizontal"
      android:layout_gravity="center_horizontal">
      <!--定義兩個單選按鈕-->
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/male"
        android:text="男"
        android:checked="false"/>
      <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/female"
        android:text="女"
        android:checked="false"/>
    </RadioGroup>
  </TableRow>
  <TableRow>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="喜歡的顏色"/>
    <!--定義一個垂直線性佈局-->
    <LinearLayout
      android:layout_gravity="center_horizontal"
      android:orientation="vertical"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
      <!--定義三個複選框-->
      <CheckBox
        android:id="@+id/color_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="紅色"/>
      <CheckBox
        android:id="@+id/color_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="藍色"/>
      <CheckBox
        android:id="@+id/color_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="綠色"/>
    </LinearLayout>
  </TableRow>
  <TextView
    android:id="@+id/show_sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  <Button
    android:id="@+id/show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="顯示覆選框內容"
    android:textSize="20pt"/>
  <TextView
    android:id="@+id/show_color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</TableLayout>
public class Home extends AppCompatActivity {
  RadioGroup radioGroup01 ;
  TextView textView01 ;
  TextView textView02 ;
  Button button01 ;
  CheckBox checkBox01 ;
  CheckBox checkBox02 ;
  CheckBox checkBox03 ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);//顯示manLayout
    //連接組建
    radioGroup01 = (RadioGroup) findViewById(R.id.rg);
    textView01 = (TextView) findViewById(R.id.show_sex);
    textView02 = (TextView) findViewById(R.id.show_color);
    checkBox01 = (CheckBox) findViewById(R.id.color_red);
    checkBox02 = (CheckBox) findViewById(R.id.color_blue);
    checkBox03 = (CheckBox) findViewById(R.id.color_green);
    button01 = (Button) findViewById(R.id.show);
    //添加監聽事件
    radioGroup01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        //根據用戶勾選信息改變tip字符串的值
        String tip = checkedId == R.id.male ?
        "您的性別爲男" : "您的性別爲n女" ;
        //修改show組件文本
        textView01.setText(tip);
      }
    });
    //輸出按鈕監聽事件
    button01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        textView02.setText("喜歡的顏色: \n");
        //篩選複選框信息
        StringBuffer stringBuffer01 = new StringBuffer();
        stringBuffer01.append(textView02.getText().toString());
        if (checkBox01.isChecked()) {
          stringBuffer01.append("紅色\n");
        }
        if (checkBox02.isChecked()) {
          stringBuffer01.append("藍色\n");
        }
        if (checkBox03.isChecked()) {
          stringBuffer01.append("綠色");
        }
        textView02.setText(stringBuffer01.toString());
      }
    });
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章