码迷,mamicode.com
首页 > 移动开发 > 详细

android单选框和复选框(练习)

时间:2016-03-28 23:12:57      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:

<?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:orientation="vertical"
    android:padding="10dp"
    tools:context="com.example.dell.test5.UiActivity1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择Android的开发语言"/>
    <RadioGroup
        android:layout_width="wrap_content"
        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"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C"
            android:id="@+id/rb2"
            android:layout_marginRight="30dp"
            android:checked="true"/>

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

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

 

 

 

package com.example.dell.test5;

import android.os.Bundle;
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() {

            @Override
            public void onCheckedChanged(RadioGroup group, 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_LONG).show();
            }
        });


        CheckBox cb_st = (CheckBox)findViewById(R.id.cb_st);

        cb_st.setOnCheckedChangeListener(new CBOnCheckedChangeListenner());

        CheckBox cb_jc = (CheckBox)findViewById(R.id.cb_jc);

        cb_jc.setOnCheckedChangeListener(new CBOnCheckedChangeListenner());

        CheckBox cb_xt = (CheckBox)findViewById(R.id.cb_xt);

        cb_xt.setOnCheckedChangeListener(new CBOnCheckedChangeListenner());

        CheckBox cb_xhx = (CheckBox)findViewById(R.id.cb_xhx);

        cb_xhx.setOnCheckedChangeListener(new CBOnCheckedChangeListenner());


    }

    private class CBOnCheckedChangeListenner 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_SHORT).show();

            }
            else
            {

                Toast.makeText(UiActivity1.this, "取消了" + cb.getText(), Toast.LENGTH_SHORT).show();
            }

            }
    }

}

技术分享

android单选框和复选框(练习)

标签:

原文地址:http://www.cnblogs.com/youshashuosha/p/5331011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!