【发布时间】:2015-04-20 21:00:52
【问题描述】:
我已经解决了许多有关单选按钮的问题,但这已经在我脑海中萦绕了很多天。我在不同的列表视图中有 5 个单选按钮,请告诉我如何在访问其他单选按钮时取消选中任何单选按钮并获取所选单选按钮的值。 以下是代码。
custom_dialogue.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="HardcodedText,RtlHardcoded,NestedWeights,UselessParent" >
<LinearLayout
android:layout_width="350dp"
android:layout_height="400dp"
android:background="#000000"
android:orientation="vertical"
android:weightSum="8" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#E3EBED"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:text="Screen timeout"
android:textColor="#449BA4"
android:textSize="21sp" />
</LinearLayout>
<RadioGroup
android:id="@+id/rg_all"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="15 seconds"
android:textColor="#000000"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/rb_15sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="30 seconds"
android:textColor="#000000"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<RadioButton
android:id="@+id/rb_30sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="1 minute"
android:textColor="#000000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_1min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="2 minutes"
android:textColor="#000000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_2min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="5 minutes"
android:textColor="#000000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_5min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:checked="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#F5F5F5"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="9"
android:text="10 minutes"
android:textColor="#000000"
android:textSize="18sp" />
<RadioButton
android:id="@+id/rb_10min"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
</LinearLayout>
</RadioGroup>
<LinearLayout
android:id="@+id/ll_cancel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#C9DCE0"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Cancel"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
CustomDialogue.java
package com.example.coustomdialogue;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class CustomDialogue extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialogue);
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_all);
final RadioButton rb1 = (RadioButton)findViewById(R.id.rb_10min);
final RadioButton rb2 = (RadioButton)findViewById(R.id.rb_15sec);
final RadioButton rb3 = (RadioButton)findViewById(R.id.rb_1min);
final RadioButton rb4 = (RadioButton)findViewById(R.id.rb_2min);
final RadioButton rb5 = (RadioButton)findViewById(R.id.rb_30sec);
final RadioButton rb6 = (RadioButton)findViewById(R.id.rb_5min);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.rb_10min){
rg.clearCheck();
rb2.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
rb5.setChecked(false);
rb6.setChecked(false);
}else if(checkedId == R.id.rb_15sec){
rg.clearCheck();
rb1.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
rb5.setChecked(false);
rb6.setChecked(false);
}else if(checkedId == R.id.rb_1min){
rg.clearCheck();
rb2.setChecked(false);
rb4.setChecked(false);
rb1.setChecked(false);
rb5.setChecked(false);
rb6.setChecked(false);
}else if(checkedId == R.id.rb_2min){
rg.clearCheck();
rb2.setChecked(false);
rb3.setChecked(false);
rb5.setChecked(false);
rb1.setChecked(false);
rb6.setChecked(false);
}else if(checkedId == R.id.rb_30sec){
rg.clearCheck();
rb2.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
rb6.setChecked(false);
rb1.setChecked(false);
}else{
rg.clearCheck();
rb2.setChecked(false);
rb2.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
rb5.setChecked(false);
rb1.setChecked(false);
}
}
});
}
}
【问题讨论】:
-
愿这能给你一些想法wptrafficanalyzer.in/blog/…
标签: android android-layout radio-button radio-group