我有一个简单的活动,我想做的是在单个单选组中显示 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);
}
}