特点:只读按钮,配合RadioGroup来使用 形成按钮
属性:button="@null" //去符号
drawableTop = “@mipmap/bq6” //图标
enabled=“false” //是否可编辑
checked = “true” //是否能选中
方法:
isChecked();//判断是否被选中
setChecked(true)//设置是否被选中
事件:
serOnCheckedChangeLisener()//这个事件带isChecked参数
属性:checked = “true” //被选中
方法:
isChecked(false)//判断是否被选中
setChecked(false)//设置不被选中
事件:
serOnCheckedChangeLisener()//这个事件带isChecked参数
// An highlighted block
<?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=".Radio_lianxiActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp">
<ImageView
android:id="@+id/imagess"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/xiuqin"/>
</LinearLayout>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submit"
android:text="提交" />
<RelativeLayout
android:layout_width="300dp"
android:layout_height="200dp">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择喜欢的食物"/>
<CheckBox
android:id="@+id/hanbao1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汉堡1"
android:drawableLeft="@mipmap/tt1"
android:layout_below="@id/text1"/>
<CheckBox
android:id="@+id/hanbao2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汉堡2"
android:drawableLeft="@mipmap/tt1"
android:layout_toRightOf="@id/hanbao1"
android:layout_marginTop="20dp"/>
<CheckBox
android:id="@+id/hanbao3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汉堡3"
android:drawableLeft="@mipmap/tt1"
android:layout_below="@id/hanbao1"/>
<CheckBox
android:id="@+id/hanbao4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="汉堡4"
android:drawableLeft="@mipmap/tt1"
android:layout_below="@id/hanbao1"
android:layout_toRightOf="@id/hanbao3"/>
<CheckBox
android:id="@+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:layout_below="@id/hanbao3"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/all"
android:text="选中的内容有"/>
</RelativeLayout>
</LinearLayout>
//JAVA代码
package com.example.tx.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class Radio_lianxiActivity extends AppCompatActivity {
RadioButton boy;
RadioButton girl;
ImageView images;
CheckBox checkBox1;
CheckBox checkBox2;
CheckBox checkBox3;
CheckBox checkBox4;
TextView textView;
CheckBox allbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_lianxi);
boy = findViewById(R.id.man);
girl = findViewById(R.id.woman);
images = findViewById(R.id.imagess);
checkBox1 = findViewById(R.id.hanbao1);
checkBox2 = findViewById(R.id.hanbao2);
checkBox3 = findViewById(R.id.hanbao3);
checkBox4 = findViewById(R.id.hanbao4);
textView = findViewById(R.id.text2);
allbox = findViewById(R.id.all);
boy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
images.setImageResource(R.mipmap.imgcat);
}
}
});
girl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
images.setImageResource(R.mipmap.aiguang);
}
}
});
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
textView.append(checkBox1.getText().toString());
}
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
textView.append(checkBox2.getText().toString());
}
}
});
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
textView.append(checkBox3.getText().toString());
}
}
});
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
textView.append(checkBox4.getText().toString());
}
}
});
allbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkBox1.setChecked(isChecked);
checkBox2.setChecked(isChecked);
checkBox3.setChecked(isChecked);
checkBox4.setChecked(isChecked);
if(isChecked){
// textView.append(checkBox1.getText().toString());
// textView.append(checkBox2.getText().toString());
// textView.append(checkBox3.getText().toString());
// textView.append(checkBox4.getText().toString());
}else{
textView.setText("");
}
}
});
}
public void submit(View view){
/**
* 单价提交时做什么
* 获得当前选中按钮的文本
*/
if(boy.isChecked()==true){
Toast.makeText(this,boy.getText().toString(), Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,girl.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
}