在Android中使用多个Radio Group值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Android中使用多个Radio Group值相关的知识,希望对你有一定的参考价值。
我有两个无线电组,每组包含两个单选按钮。我想等到每个组中的按钮被选中,然后显示Toast。 (例如,如果选择组1中的单选按钮1和组2中的单选按钮3,则显示Toast消息。我没有收到任何错误消息,但是没有Toast显示:
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb2" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="228dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb3" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="@string/rb4" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
JAVA代码:
package com.example.rgapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (view.getId() == (R.id.rb1) & (view.getId() == (R.id.rb3)))
Toast.makeText(getApplicationContext(), "1 and 3 selected",
Toast.LENGTH_SHORT).show();
}
}
答案
在xml和java下面试试这个
activity_main.xml中
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="92dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.508"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb2" />
</RadioGroup>
<RadioGroup
android:id="@+id/rg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="228dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb3" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onCustomClick"
android:text="rb4" />
</RadioGroup>
</android.support.constraint.ConstraintLayout>
main activity.Java :
public class MainActivity extends Activity {
RadioButton rb1, rb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb3 = (RadioButton) findViewById(R.id.rb3);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCustomClick(v);
}
});
}
public void onCustomClick(View view) {
boolean checked = ((RadioButton) view).isChecked();
if (rb1.isChecked() && rb3.isChecked())
Toast.makeText(getApplicationContext(), "1 and 3 selected",
Toast.LENGTH_SHORT).show();
}
}
如果选择来自第1组的radiobutton1
和来自第2组的radiobutton3
,它将显示吐司。
另一答案
如果选择了无线电组,那么请使用此代码来实现您的功能,然后您将获得结果
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
RadioGroup rg1,rg2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg1 =(RadioGroup)findViewById(R.id.rg1);
rg2 =(RadioGroup)findViewById(R.id.rg2);
rg1.setOnCheckedChangeListener(this);
rg2.setOnCheckedChangeListener(this);
}
int radioId1 = 0,radioId2 = 0;
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (radioGroup == rg1){
radioId1 = i;
}
if (radioGroup == rg2){
radioId2 = i;
}
if (radioId1 != 0 && radioId2 != 0){
RadioButton radioButton1 = (RadioButton) findViewById(radioId1);
RadioButton radioButton2 = (RadioButton) findViewById(radioId2);
Toast.makeText(this,radioButton1.getText()+" and "+radioButton2.getText()+" is selected",Toast.LENGTH_LONG).show();
}
}
}
以上是关于在Android中使用多个Radio Group值的主要内容,如果未能解决你的问题,请参考以下文章
如何使用多个 b-form-radio-group 避免它们之间的视觉干扰?
Android:为啥 Radio Group 中的 Radio Button 总是为任何选定的项目返回 false?
MFC窗口应用程序中如何向Group box中添加多个radio button?