activity_ui1.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:padding="10dp" android:orientation="vertical" tools:context="com.hanqi.application3.UIActivity1" android:weightSum="1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择Android的开发语言是什么"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/rb" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C++" android:id="@+id/rb1" android:layout_marginRight="30dp" android:checked="true" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C" android:id="@+id/rb2" android:layout_marginRight="30dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="JAVA" android:id="@+id/rb3" android:layout_marginRight="30dp" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C#" android:id="@+id/rb4" android:layout_marginRight="30dp" /> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择字体效果"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="宋体" android:id="@+id/cb_st" android:checked="true" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加粗" android:id="@+id/cb_jc" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="斜体" android:id="@+id/cb_xt" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下划线" android:id="@+id/cb_xhx" /> </LinearLayout>
UIActivity1.java
package com.hanqi.application3; import android.app.Activity; import android.os.Bundle; import android.support.annotation.IdRes; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import static android.widget.CompoundButton.*; public class UIActivity1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ui1); RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rb); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { if (checkedId == R.id.rb3) { Toast.makeText(UIActivity1.this, "选对了", Toast.LENGTH_SHORT).show(); } RadioButton rb = (RadioButton) findViewById(checkedId); Toast.makeText(UIActivity1.this, rb.getText(), Toast.LENGTH_SHORT).show(); } }); CheckBox cb_st= (CheckBox)findViewById(R.id.cb_st); cb_st.setOnCheckedChangeListener(new CBOnCheckChangeListenter()); CheckBox cb_xt= (CheckBox)findViewById(R.id.cb_xt); cb_xt.setOnCheckedChangeListener(new CBOnCheckChangeListenter()); CheckBox cb_jc= (CheckBox)findViewById(R.id.cb_jc); cb_jc.setOnCheckedChangeListener(new CBOnCheckChangeListenter()); CheckBox cb_xhx= (CheckBox)findViewById(R.id.cb_xhx); cb_xhx.setOnCheckedChangeListener(new CBOnCheckChangeListenter()); } private class CBOnCheckChangeListenter implements OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { CheckBox cb = (CheckBox)buttonView; if (isChecked) { Toast.makeText(UIActivity1.this, "选中了", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(UIActivity1.this, "取消了", Toast.LENGTH_SHORT).show(); } } } }