实现按钮式单选

来源:互联网 发布:导出来的sql带双引号 编辑:程序博客网 时间:2024/04/26 19:32

很多时候,老大可能会要求你做出这样的单选效果,如下图:




小弟想到三种方法来实现,在此献丑总结一下。


一、最省事省力的一种

使用RadioGroup,只要轻轻地对RadioGroup进行适当的设置,就能达到这样的效果了,请见下面的xml文件:

<RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:checkedButton="@+id/btn_one"android:orientation="horizontal" ><RadioButtonandroid:id="@id/btn_one"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/selector_bg_choice"android:button="@null"android:gravity="center"android:minHeight="48dp"android:text="Male"android:textColor="@drawable/selector_text_color"android:textSize="16sp"android:textStyle="bold" /><RadioButtonandroid:id="@+id/btn_two"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/selector_bg_choice"android:button="@null"android:gravity="center"android:minHeight="48dp"android:text="Female"android:textColor="@drawable/selector_text_color"android:textSize="16sp"android:textStyle="bold" /></RadioGroup>

其中,使用到的两个selector,如下:

背景颜色:selector_bg_choice.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 点击用 --><item android:drawable="@color/green" android:state_pressed="true"/><!-- CheckBox/RadioButton用 --><item android:drawable="@color/green" android:state_checked="true"/><!-- TabWidget用 --><item android:drawable="@color/green" android:state_selected="true"/><item android:drawable="@color/white"/></selector>

文字颜色:selector_text_color.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_pressed="true" android:color="@color/white"/><item android:state_checked="true" android:color="@color/white"/><item android:state_selected="true" android:color="@color/white"/><item android:color="@color/black"/></selector>

PS:看了大家会发现,两个selector中,为啥一个是android:drawable另一个是android:color呢?

这是由使用到它的地方决定的,第一个selector使用在android:backgroud属性,这个属性需要一个drawable;而第二个selector使用在android:textColor,这个属性需要一个color。


二、较省力的一种

使用多个CheckBox(或者ToggleButton),布局如下:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:orientation="horizontal" ><CheckBoxandroid:id="@+id/cb_one"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/selector_bg_choice"android:button="@null"android:checked="true"android:gravity="center"android:minHeight="48dp"android:text="Male"android:textColor="@drawable/selector_text_color"android:textSize="16sp"android:textStyle="bold" /><CheckBoxandroid:id="@+id/cb_two"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/selector_bg_choice"android:button="@null"android:gravity="center"android:minHeight="48dp"android:text="Female"android:textColor="@drawable/selector_text_color"android:textSize="16sp"android:textStyle="bold" /><CheckBoxandroid:id="@+id/cb_three"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/selector_bg_choice"android:button="@null"android:gravity="center"android:minHeight="48dp"android:text="None"android:textColor="@drawable/selector_text_color"android:textSize="16sp"android:textStyle="bold" /></LinearLayout>

但还需要在程序里进行一些小设置:

// 第二种方法:使用多个CheckBox实现CheckBox cb1 = (CheckBox) findViewById(R.id.cb_one);CheckBox cb2 = (CheckBox) findViewById(R.id.cb_two);CheckBox cb3 = (CheckBox) findViewById(R.id.cb_three);cb1.setOnClickListener(this);cb2.setOnClickListener(this);cb3.setOnClickListener(this);mChecks = new CheckBox[] { cb1, cb2, cb3 };

@Overridepublic void onClick(View v) {for (CheckBox cb : mChecks) {cb.setChecked(v == cb);}}
 在程序中,需要保证只有一个CheckBox被check,就酱。


三、比较蛋碎的一种实现,但也能达到效果。

就是使用TabHost/TabWidget

<TabHostandroid:id="@+id/th_tabhost"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="8dp" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" /><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone" /></TabHost>

// 第三种方法:使用TabHost实现TabHost tabhost = (TabHost) findViewById(R.id.th_tabhost);tabhost.setup();tabhost.addTab(tabhost.newTabSpec("male").setIndicator(newTab("Male")).setContent(mTabContentFactory));tabhost.addTab(tabhost.newTabSpec("female").setIndicator(newTab("Female")).setContent(mTabContentFactory));}private TabContentFactory mTabContentFactory = new TabContentFactory() {@Overridepublic View createTabContent(String tag) {return new View(MainActivity.this);}};private View newTab(String text) {LayoutInflater inflater = LayoutInflater.from(this);TextView tv = (TextView) inflater.inflate(R.layout.layout_tab, null);tv.setText(text);return tv;}

这种方法就有点浪费资源了,毕竟TabHost不是为了实现这么一个小功能的...



好吧,写这篇文章,其一的目的是想让大家了解下selector的作用,这可是个好东东啊,不要拘泥于eclipse提示的属性,除了drawable,还可以写别的属性的!再者,就是希望大家多去了解Android原生的控件和它们的属性,用原生的组件变化、组合成自己想要的控件,也是一件很有意思的事情。


demo下载:http://download.csdn.net/detail/ueryueryuery/7729883


0 0
原创粉丝点击