• 安卓单选、复选按钮简单练习


    xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp">
    
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你最爱吃的食物是?"
            />
    
        <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="茅坑土"
                android:id="@+id/mk"
                android:layout_marginRight="20dp"
                android:checked="true"/>
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="墙头草"
                android:id="@+id/qc"
                android:layout_marginRight="20dp"/>
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="窝里粪"
                android:id="@+id/wf"
                android:layout_marginRight="20dp"/>
    
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="臭沟油"
                android:id="@+id/cy"
                />
    
    
    
        </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/cl"
            android:checked="true"/>
    
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小瘪三"
            android:id="@+id/bs"/>
    
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="坑爹呀"
            android:id="@+id/kd"/>
    
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="唉呀妈呀"
            android:id="@+id/am"/>
    
    </LinearLayout>

    Java代码

    package com.hanqi.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;
    
    /**
     * Created by Administrator on 2016/3/28.
     */
    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.wf) {
                        Toast.makeText(UIActivity1.this, "你选对了", Toast.LENGTH_SHORT).show();
                    }
    
                    RadioButton rb = (RadioButton) findViewById(checkedId);
    
                    Toast.makeText(UIActivity1.this, rb.getText(), Toast.LENGTH_SHORT).show();
    
                }
            });
    
    
            CheckBox cl = (CheckBox)findViewById(R.id.cl);
    
            cl.setOnCheckedChangeListener(new CBOnCheckedChangListenter());
    
            CheckBox bs = (CheckBox)findViewById(R.id.bs);
    
            bs.setOnCheckedChangeListener(new CBOnCheckedChangListenter());
    
            CheckBox kd = (CheckBox)findViewById(R.id.kd);
    
            kd.setOnCheckedChangeListener(new CBOnCheckedChangListenter());
    
            CheckBox am = (CheckBox)findViewById(R.id.am);
    
            am.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_SHORT).show();
                }
                else
                {
                    Toast.makeText(UIActivity1.this, "取消了" + cb.getText(), Toast.LENGTH_SHORT).show();
    
                }
            }
        }
    }

  • 相关阅读:
    [转载]使用SecureCRT进行vim编辑的时候,小键盘变成字母的解决办法
    [转载]运行中的程序突然出现"Killed"原因
    [转载]IPv6地址表示方法详解
    【转载】深入浅出Pairwise算法
    [转载]不要用强制方法杀掉python线程
    [转载]Python SQLite3的问题sqlite3.ProgrammingError: SQLite objects created in a thread
    SQL语句中distinct的分页和查询数据数量
    SQL语句中获取时间的方法
    SQLService中使用SQL语句书写分页
    使用Angular下拉自动加载
  • 原文地址:https://www.cnblogs.com/fangchongyan/p/5330874.html
Copyright © 2020-2023  润新知