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