zoukankan      html  css  js  c++  java
  • andorid 单选与复选

    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();
                }
            }
        }
    }
  • 相关阅读:
    【转】工作流持久化的几点说明
    转:壹百度-百度十年千倍的29条法则
    CRM软件设计评测点与采集测评点
    键盘上各按键对应的ASSII值
    导入Excle数据至数据库 “外部表不是预期的格式”错误信息
    浅谈代码的执行效率(2):编译器的威力 [摘自赵劼老师的博客]
    代码的执行效率(3)缓存与局部性 摘自赵劼老师的博客
    浅谈代码的执行效率(一)
    C# Base64加解密图片
    Bulk Insert的用法
  • 原文地址:https://www.cnblogs.com/cuikang/p/5331358.html
Copyright © 2011-2022 走看看