01、单项选择 RadioGrope RadioButton
单选xml代码
<?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"
android:orientation="vertical"
tools:context="com.example.sky.myapplication.SingleChooseActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv"
android:textSize="22dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="38dp">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rb1"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="38dp" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rb2"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="38dp" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rb3"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="38dp" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rb4"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="38dp" />
</RadioGroup>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnSingleChoose" />
</LinearLayout>
SingleChooseActivity.class
package com.example.sky.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class SingleChooseActivity extends AppCompatActivity {
//声明提交按钮 正确选项
private Button btnSubmit;
private RadioButton rb4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例提交按钮 正确选项
btnSubmit = findViewById(R.id.btnSubmit);
rb4 = findViewById(R.id.rb4);
//侦听提交按钮
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//做出判断
if(rb4.isChecked()){
Toast.makeText(SingleChooseActivity.this,R.string.right,Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(SingleChooseActivity.this,R.string.wrong,Toast.LENGTH_SHORT).show();
}
}
});
}
}
strings.xml
<resources>
//两个小插件用的1个strings.xml
//单选
<string name="app_name">My Application</string>
<string name="rb1">西游记</string>
<string name="rb2">红楼梦</string>
<string name="rb3">水浒传</string>
<string name="rb4">斗破苍穹</string>
<string name="tv">哪个不是四大名著里面的?</string>
<string name="btnSingleChoose">提交</string>
<string name="right">选对了</string>
<string name="wrong">选错了</string>
//多选
<string name="tvCheck">下面哪些是四大名著里面的</string>
<string name="cb1">西游记</string>
<string name="cb2">斗破苍穹</string>
<string name="cb3">红楼梦</string>
<string name="cb4">盘龙</string>
<string name="cb5">斗罗大陆</string>
<string name="btnCheck">提交</string>
</resources>
02、多项选择代码 CheckBox
activity_check_box.xml
<?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"
android:orientation="vertical"
tools:context="com.example.sky.myapplication.CheckBoxActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/tvCheck"
android:textSize="22dp" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cb1" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cb2" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cb3" />
<CheckBox
android:id="@+id/cb4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cb4" />
<CheckBox
android:id="@+id/cb5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cb5" />
<Button
android:id="@+id/btn_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnCheck" />
</LinearLayout>
CheckBoxActivity.class
package com.example.sky.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
// 声明提交按钮 多选项
private Button btn_cb;
private CheckBox cb1,cb2,cb3,cb4,cb5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
//实例提交按钮 多选项
btn_cb = findViewById(R.id.btn_cb);
cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
cb4 = findViewById(R.id.cb4);
cb5 = findViewById(R.id.cb5);
//多选项侦听
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
cb4.setOnCheckedChangeListener(this);
cb5.setOnCheckedChangeListener(this);
//侦听提交按钮
btn_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//做出判断
if( (cb1.isChecked() && cb3.isChecked() ) && !cb2.isChecked() && !cb4.isChecked() && !cb5.isChecked()){
Toast.makeText(CheckBoxActivity.this,R.string.right,Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(CheckBoxActivity.this,R.string.wrong,Toast.LENGTH_SHORT).show();
};
}
});
}
//多选项check接口
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}