android radio(单选框) 两个单选框绑定
来源:互联网 发布:什么小说软件免费 编辑:程序博客网 时间:2024/06/07 04:30
在程序中会用到监听事件,监听到一个单选框被点击后,另一个单选框也会被选上。当然,这两个点选框不是在一组中的。
同样,在MainActivity.java文件中:
package com.yx.radio;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton femaleButton;
private RadioButton maleButton;
private RadioGroup radioGroup2;
private RadioButton femaleButton2;
private RadioButton maleButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup)findViewById(R.id.radioGroupId);
femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);
maleButton = (RadioButton)findViewById(R.id.maleButtonId);
radioGroup2 = (RadioGroup)findViewById(R.id.radioGroup2Id);
femaleButton2 = (RadioButton)findViewById(R.id.femaleButton2Id);
maleButton2 = (RadioButton)findViewById(R.id.maleButton2Id);
RadioGroupListener rgl = new RadioGroupListener();
radioGroup.setOnCheckedChangeListener(rgl);
radioGroup2.setOnCheckedChangeListener(rgl);
}
class RadioGroupListener implements OnCheckedChangeListener{//设置监听,需要实现OnCheckedChangeListener接口(这个应该是内部类)
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if(arg1 == femaleButton.getId()){
femaleButton2.setChecked(true);
}
else if(arg1 == maleButton.getId()){
maleButton2.setChecked(true);
}
else if(arg1 == femaleButton2.getId()){
femaleButton.setChecked(true);
}
else if(arg1 == maleButton2.getId()){
maleButton.setChecked(true);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
在activity_main.xml文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/radioGroupId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/femaleButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="female"
/>
<RadioButton
android:id="@+id/maleButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male"
/>
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroup2Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/femaleButton2Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="female2"
/>
<RadioButton
android:id="@+id/maleButton2Id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male2"
/>
</RadioGroup>
</LinearLayout>
- android radio(单选框) 两个单选框绑定
- jquery单选框radio绑定click事件实现方法
- radio单选框
- radio button 绑定数据
- <s:radio标签绑定
- radio 绑定标签
- 【jQuery】操作单选框 radio
- 单选框radio取值
- js操作单选框radio
- 一款单选框radio样式
- 单选框radio改变事件
- angularjs radio单选框
- 安卓初学:Radio单选框和checkBox多选控件的主要用法以及事件绑定总结
- radio 切换效果 -JQuery绑定事件.bind() 绑定radio值
- HTML 中多个Radio 的绑定
- JQuery 绑定事件.bind() 绑定radio值
- Radio Button(单选框) 的使用
- js radio单选框使用总结
- [go语言]基础知识
- Android Camera OMXCameraAdapter.cpp初始化分析
- 配置 SNAT 和NAT
- 构建高性能的ASP.NET应用(二)-性能优化演绎法
- 一个好用的python代码开发工具——sublime text2
- android radio(单选框) 两个单选框绑定
- rails入门(3)-- 数据库操作
- win7 破解后找不到活动分区以及bootmsr missing
- lfs安装遇到的问题
- 建立视图
- [go语言]基本数据类型
- 跟我一起玩Win32开发(23):渐变颜色填充
- 构建高性能的ASP.NET应用(三)-从监控出发,让一切用数据说话
- Keil MDK编译器(V4.03)与J-LINK使用