[英]how to group multiple radio button under radio group
我有一個簡單的活動,我想做的是在單個廣播組中顯示6個單選按鈕
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/adbms_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:layout_marginTop="10dp"
android:text="@string/adbms" />
<RadioButton
android:id="@+id/we_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="@string/we" />
<RadioButton
android:id="@+id/mp_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="30dp"
android:text="@string/mp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/tcs_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/tcs"
/>
<RadioButton
android:id="@+id/evs_radioBtn"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="56dp"
android:layout_height="wrap_content"
android:text="@string/evs" />
<RadioButton
android:id="@+id/cn_radioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="22dp"
android:text="@string/cn" ></RadioButton>
</TableRow>
</RadioGroup>
</LinearLayout>
但是它不起作用,這意味着我可以選擇所有單選按鈕,但是我只想選擇一個單選按鈕,選擇另一個單選按鈕應該取消選擇第一個單選按鈕,請問有沒有人可以幫忙?
我已經對此進行了編碼,以實現類似無線電組的功能,但是,當我選擇多個單選按鈕時,它卻無法按預期工作
package com.example.istudy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class AddMenuScreen extends Activity implements OnClickListener, OnCheckedChangeListener{
private RadioButton adbmsRadioBtn;
private RadioButton cnRadioBtn;
private RadioButton tcsRadioBtn;
private RadioButton evsRadioBtn;
private RadioButton mpRadioBtn;
private RadioButton weRadioBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_activity);
Button doneBtn = (Button) findViewById(R.id.doneBtn);
doneBtn.setOnClickListener(this);
adbmsRadioBtn = (RadioButton) findViewById(R.id.adbms_radioBtn);
weRadioBtn = (RadioButton) findViewById(R.id.we_radioBtn);
mpRadioBtn = (RadioButton) findViewById(R.id.mp_radioBtn);
evsRadioBtn = (RadioButton) findViewById(R.id.evs_radioBtn);
tcsRadioBtn = (RadioButton) findViewById(R.id.tcs_radioBtn);
cnRadioBtn = (RadioButton) findViewById(R.id.cn_radioBtn);
adbmsRadioBtn.setOnCheckedChangeListener(this);
cnRadioBtn.setOnCheckedChangeListener(this);
mpRadioBtn.setOnCheckedChangeListener(this);
weRadioBtn.setOnCheckedChangeListener(this);
evsRadioBtn.setOnCheckedChangeListener(this);
tcsRadioBtn.setOnCheckedChangeListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.doneBtn:
saveData( );
// Toast.makeText(AddMenuScreen.this, "done btn clicked..", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
private void saveData() {
// TODO Auto-generated method stub
EditText chaptersCompletedTxtBox = (EditText) findViewById(R.id.chaptersCompletedTxtBox);
int chaptersCompleted = Integer.parseInt( chaptersCompletedTxtBox.getText().toString() );
if( adbmsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, adbmsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For ADBMS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( weRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, weRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For WE valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( mpRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 8 ){
insertIntoDB(chaptersCompleted, mpRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For MP valid range is 1-7", Toast.LENGTH_SHORT).show();
}
}else if( evsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, evsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For EVS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( tcsRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, tcsRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For TCS valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}else if( cnRadioBtn.isChecked() ){
if( chaptersCompleted > 0 && chaptersCompleted < 9 ){
insertIntoDB(chaptersCompleted, cnRadioBtn.getText().toString());
}else{
Toast.makeText(AddMenuScreen.this, "For CN valid range is 1-8", Toast.LENGTH_SHORT).show();
}
}
}
private void insertIntoDB(int chaptersCompleted, String subName) {
// TODO Auto-generated method stub
DBHandler db = new DBHandler(this);
db.updateSem5Table(chaptersCompleted, subName);
Toast.makeText(AddMenuScreen.this, "Data saved successfully", Toast.LENGTH_SHORT).show();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
deselectAllRadioBtn();
buttonView.setChecked(true);
Toast.makeText(this, buttonView.getText().toString() + " is checked", Toast.LENGTH_SHORT).show();
}
private void deselectAllRadioBtn() {
// TODO Auto-generated method stub
adbmsRadioBtn.setChecked(false);
cnRadioBtn.setChecked(false);
evsRadioBtn.setChecked(false);
tcsRadioBtn.setChecked(false);
mpRadioBtn.setChecked(false);
weRadioBtn.setChecked(false);
}
}
在我看來,好像您的RadioGroup包含TableRows而不是RadioButtons。 如果將RadioButtons放在應該起作用的RadioGroup下的一個級別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.