RadioButton是单选按钮,但实际上,需要RadioGroup包含才能实现单选,而且在实践中发现即使RadioButton在同一个RadioGroup下,但内嵌在布局中,都不能实现单选的作用,如下:
下面时实现单选按钮的方法:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"/>
</RadioGroup>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
实现单选后,需要获取选取的是哪个按钮,则需要一下代码:
package com.example.administrator.myapplication;
import android.content.BroadcastReceiver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private RadioButton rb1,rb2;
private Button btn1;
private int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
findinit();
//利用RadioGroup的监听方式
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (group.getCheckedRadioButtonId()){
case R.id.rb1: Log.d("RadioGroup","1"); break;
case R.id.rb2: Log.d("RadioGroup","2"); break;
}
}
});
//使用按钮监听的方法
rb1.setOnClickListener(onClick);
rb2.setOnClickListener(onClick);
btn1.setOnClickListener(onClick);
}
public void findinit(){
rg=findViewById(R.id.rg1);
//使用RadioGroup的监听方式时,rb1,rb2可省略,并无发生任何错误
//这里加上为了使用Button监听时
rb1=findViewById(R.id.rb1);
rb2=findViewById(R.id.rb2);
btn1=findViewById(R.id.btn1);
}
//使用按钮监听的方法
private View.OnClickListener onClick=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.rb1:i=1; break;
case R.id.rb2:i=2; break;
case R.id.btn1:
if(i==1){
Log.d("Button","1");
}else if(i==2){
Log.d("Button","2");
}
break;
}
}
};
}