自定义RadioButton
来源:互联网 发布:php 字符型转变为数值 编辑:程序博客网 时间:2024/05/29 07:08
Android提供的RadioButton样式和功能十分有限,在使用多个RadioButton进行单选的时候,RadioGroup并不好用,比如说它只能横着或者竖着排,却不能用其他排列方式。在这里自己没事瞎写一个自定义的RadioButton,实现效果如下:
源码如下:RadioButtonActivity.java
import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.CompoundButton.OnCheckedChangeListener;public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{RadioButton rb1;RadioButton rb2;RadioButton rb3;RadioButton rb4; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rb1 = (RadioButton)findViewById(R.id.radioButton1); rb2 = (RadioButton)findViewById(R.id.radioButton2); rb3 = (RadioButton)findViewById(R.id.radioButton3); rb4 = (RadioButton)findViewById(R.id.radioButton4); rb1.setOnCheckedChangeListener(this); rb2.setOnCheckedChangeListener(this); rb3.setOnCheckedChangeListener(this); rb4.setOnCheckedChangeListener(this); } @Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubswitch(buttonView.getId()){case R.id.radioButton1:if(isChecked == true){rb1.setChecked(isChecked);rb2.setChecked(!isChecked);rb3.setChecked(!isChecked);rb4.setChecked(!isChecked);isChecked = false;}break;case R.id.radioButton2:if(isChecked == true){rb1.setChecked(!isChecked);rb2.setChecked(isChecked);rb3.setChecked(!isChecked);rb4.setChecked(!isChecked);isChecked = false;}break;case R.id.radioButton3:if(isChecked == true){rb1.setChecked(!isChecked);rb2.setChecked(!isChecked);rb3.setChecked(isChecked);rb4.setChecked(!isChecked);isChecked = false;}break;case R.id.radioButton4:if(isChecked == true){rb1.setChecked(!isChecked);rb2.setChecked(!isChecked);rb3.setChecked(!isChecked);rb4.setChecked(isChecked);isChecked = false;}break;default:break;}}}
Blog地址http://blog.csdn.net/kira012345/article/details/6585125
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:orientation="horizontal"> <RadioButton android:text="RadioButton1" android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton> <RadioButton android:text="RadioButton2" android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="horizontal"> <RadioButton android:text="RadioButton3" android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton> <RadioButton android:text="RadioButton4" android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false"></RadioButton> </LinearLayout></LinearLayout>
复制粘贴即可测试。
- RadioButton与自定义RadioButton
- 自定义RadioButton
- 自定义Radiobutton
- 自定义radiobutton
- 自定义RadioButton
- RadioButton 自定义
- 自定义RadioButton
- 自定义RadioButton
- 自定义RadioButton
- RadioButton - android 自定义RadioButton样式
- 自定义RadioButton样式1
- 自定义RadioButton样式
- RadioButton自定义样式
- 自定义 RadioButton 图片
- android 自定义RadioButton样式
- Android RadioButton的自定义
- 自定义RadioButton样式2
- 自定义RadioButton样式
- Activiti5.6 安装记录
- 教你用C#读写、删除、更新excel表格记录
- 编程小技巧
- WdatePicker日历控件使用方法
- oracle 启动关闭以及监听启动关闭命令
- 自定义RadioButton
- YUV422 to RGB 转换
- exe4j把jar打包成exe文件,运行报错的解决办法!!!
- iphone学习笔记--使用http与服务器端通信
- usaco3.2kimbits
- mysql绿化
- WIN7上运行PB5.0
- SQL ISNULL ( check_expression , replacement_value )
- 学习Objective-C:入门手册