RadioButton 和 CheckBox

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.xiesir.example08radiobuttoncheckbox.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一、基本用法与事件处理\n 1) RadioButton\n 要把RadioButton放到RadioGroup中实现单选功能!\n RadioGroup的orientation属性设置排列方式" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择性别" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/btnMan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:paddingLeft="10dp" android:checked="true" /> <!--android:button="@null"--> <!--android:drawableBottom="@mipmap/ic_launcher"--> <RadioButton android:id="@+id/btnWoman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android:paddingLeft="10dp" android:text="女" /> </RadioGroup> <Button android:id="@+id/btnpost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2) CheckBox\n 复选框可以同时选中多个选项" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你掌握的编程语言" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cbc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C/C++" /> <CheckBox android:id="@+id/cbj" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" /> <CheckBox android:id="@+id/cbcs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C#" /> <CheckBox android:id="@+id/cbo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="其它" /> </LinearLayout> <Button android:id="@+id/btncheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout>
MainActivity.java:
package com.xiesir.example08radiobuttoncheckbox; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Toast; public class MainActivity extends AppCompatActivity { CheckBox cbc, cbj, cbcs, cbo; Button btncheck; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RadioGroup rgroup = (RadioGroup) findViewById(R.id.radioGroup); // 第一种获得单选按钮值的方法 // 为radioGroup设置一个监听器:setOnCheckedChanged() rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rbtn = (RadioButton) findViewById(checkedId); Toast.makeText(getApplicationContext(), "单选按钮组值发生改变,你选了: " + rbtn.getText(), Toast.LENGTH_LONG).show(); } }); Button btnpost = (Button) findViewById(R.id.btnpost); // 为按钮设置一个监听器 btnpost.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < rgroup.getChildCount(); i++) { RadioButton rb = (RadioButton) rgroup.getChildAt(i); if (rb.isChecked()) { Toast.makeText(getApplicationContext(), "点击提交按钮, 你选了: " + rb.getText(), Toast.LENGTH_LONG).show(); break; } } } }); cbc = (CheckBox) findViewById(R.id.cbc); cbj = (CheckBox) findViewById(R.id.cbj); cbcs = (CheckBox) findViewById(R.id.cbcs); cbo = (CheckBox) findViewById(R.id.cbo); btncheck = (Button) findViewById(R.id.btncheck); cbc.setOnCheckedChangeListener(new MyCheckListener()); cbj.setOnCheckedChangeListener(new MyCheckListener()); cbcs.setOnCheckedChangeListener(new MyCheckListener()); cbo.setOnCheckedChangeListener(new MyCheckListener()); btncheck.setOnClickListener(new MyButtonListener()); } class MyCheckListener implements CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (buttonView.isChecked()) Toast.makeText(getApplicationContext(), buttonView.getText().toString(), Toast.LENGTH_LONG).show(); } } class MyButtonListener implements View.OnClickListener { @Override public void onClick(View v) { String choose = ""; if (cbc.isChecked()) choose += cbc.getText().toString() + " "; if (cbj.isChecked()) choose += cbj.getText().toString() + " "; if (cbcs.isChecked()) choose += cbcs.getText().toString() + " "; if (cbo.isChecked()) choose += cbo.getText().toString(); Toast.makeText(getApplicationContext(), choose, Toast.LENGTH_SHORT).show(); } } }
参考:
http://blog.csdn.net/coder_pig/article/details/47035625
浙公网安备 33010602011771号