Android开发 第七课 RadioGroup和RadioButton

RadioButton 按了之后就不能修改了,不能复用(不建议RadioButton 单独使用)
只能选一次
1.RadioGroup
RadioButton 的一个集合,提供多选一的机制
属性
android:orientation= “vertical” 垂直排布
“horizontal”水平排布
决定当前RadioGroup中RadioButton以什么形式排列

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.adb.li806.demo5.MainActivity"

    android:orientation="vertical"
    android:weightSum="1">


    <RadioGroup
        android:orientation="horizontal"
        android:id="@+id/radioGroup1"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="喜欢"

            android:checked="true"
            android:textSize="25sp"/>

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="不喜欢"

            android:textSize="25sp"/>
    </RadioGroup>
</LinearLayout>
package com.adb.li806.demo5;

import android.support.annotation.IdRes;
import android.support.annotation.VisibleForTesting;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;//这个OnCheckedChangeListener包的选择
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup) findViewById(R.id.radioGroup1);
        /*
        * 实现RadioGroup的监听事件
        * */

        rg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId){
            case R.id.radioButton1:
                Log.i("tag","喜欢");
                Toast.makeText(getApplicationContext(),"喜欢",Toast.LENGTH_SHORT).show();
                break;
            case R.id.radioButton2:
                Log.i("tag","不喜欢");
                Toast.makeText(getApplicationContext(),"不喜欢",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值