Android课程---单选框与复选框的实现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hanqi.test5">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".UIActivity1">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LongClickActivityActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity android:name=".CalculatorActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activity1" />


    </application>

</manifest>
package com.hanqi.test5;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class UIActivity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui1);
        //单选框
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if (checkedId == R.id.rb3) //rb3设定为正确答案
                {
                    Toast.makeText(UIActivity1.this, "选对了", Toast.LENGTH_LONG).show();
                }
                RadioButton rb = (RadioButton) findViewById(checkedId);
                Toast.makeText(UIActivity1.this, rb.getText(), Toast.LENGTH_LONG).show();
            }
        });




        //复选框
        CheckBox cb_st = (CheckBox)findViewById(R.id.cb_st);
        cb_st.setOnCheckedChangeListener(new CBOnCheckedChangListenter() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });

        cb_st.setOnCheckedChangeListener(new CBOnCheckedChangListenter());

        CheckBox cb_jc = (CheckBox)findViewById(R.id.cb_jc);
        cb_jc.setOnCheckedChangeListener(new CBOnCheckedChangListenter());

        CheckBox cb_xt = (CheckBox)findViewById(R.id.cb_xt);
        cb_xt.setOnCheckedChangeListener(new CBOnCheckedChangListenter());

        CheckBox cb_xhx = (CheckBox)findViewById(R.id.cb_xhx);
        cb_xhx.setOnCheckedChangeListener(new CBOnCheckedChangListenter());


    }
    private class CBOnCheckedChangListenter implements CompoundButton.OnCheckedChangeListener
    {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        CheckBox cb = (CheckBox)buttonView;

        if (isChecked) {
            Toast.makeText(UIActivity1.this, "选中了" + cb.getText(), Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(UIActivity1.this, "取消了" + cb.getText(), Toast.LENGTH_LONG).show();
        }
    }
    }
}
<?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.test5.UIActivity1">
    <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/rg">

        <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" />
    </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>

在虚拟机6.0版本实现时,自带动画效果很好,可以用于做调查或者考试

效果图:

 

posted @ 2016-04-03 12:24  秦萧不再  阅读(20449)  评论(1编辑  收藏  举报