RaidoGroup+RadioButton模拟android下拉框弹出List
引用
<上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边。试了各种方法都不行。
最后,无意中给RadioButton添加一个backgroud属性即可:
<RadioButton
android:id="@+id/rbAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="测试条目一"
android:textColor="@android:color/primary_text_light"
android:textSize="14sp" />
最后实现了所需效果。>
已检证OK。
上面引用的话,是被用于利用模板xml文件来实现。具体实现模板举例如下:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="warp_content">
<RadioButton
......
/>
......<!- 在这里写下复数个RadioButton ->
</RadioGroup>
这是模板,另外就是动作触发,动作触发元对象任意,比如一个按钮,触发方式很简单,用startActivityForResult()方式。
如果用上面这种模板来做的话,做固定选项可以,不过要应对选项是非固定的内容的话,例如:从数据库里检索出的List时,
就不方便了。这时候就需要在后台去生成RadioButton,并添加到RadioGroup。
Java实现:
java实现的时候RadioGroup是写在页面上的
<RadioGroup
android:id="@+id"
android:layout_width="match_parent"
android:layout_height="warp_content">
</RadioGroup>
在后台动态追加RadioButton。
做法一,代码如下:
private void initCityGroup(AddressMasterDto addressMasterDto) {
radioGroup.setVisibility(View.VISIBLE);
ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
lvDetailInfo.setVisibility(View.GONE);
resultDtoList = initCityList(addressMasterDto);
if (resultDtoList != null && resultDtoList.size() > 0) {
for (DtoBase dto : resultDtoList) {
AddressMasterDto addressDto = (AddressMasterDto) dto;
if (addressDto != null) {
RadioButton radioButton = new RadioButton(this);
// 其实ID设不设置感觉问题不大,因为本身就不被关心。并且不建议这样设置,为了保证唯一性。
//radioButton.setId(resultDtoList.indexOf(dto) + 1);
radioButton.setText(addressDto.getText());
radioButton.setTextSize(20);
radioButton.setTextColor(getResources().getColor(R.color.black));// 文字颜色android默认是白色~~,我这边背景色是白的,于是我改成黑色的才显示文字。 // 下面这四行是让文字居左,按钮居右。
radioButton.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));// 让原来左边的单选按钮不显示。
Drawable radioDrawable = getResources().getDrawable(android.R.drawable.btn_radio);
radioDrawable.setBounds(0, 0, radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());
radioButton.setCompoundDrawables(null, null, radioDrawable, null); RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
radioLayout.setMargins(-35, 0, 0, 0); // *1 默认match_parent貌似左边并没有到边,剩下的距离自行调整,我这里就用了负数来设定marginLeft来达到靠左。
radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
radioGroup.addView(radioButton); // 这里是在选项间添加了一个分割线。这个做法很好。
if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
View v = new View(this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
radioGroup.addView(v);
}
}
} // 这是为了选中添加监听事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
// 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
// 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
int index = group.indexOfChild(radioButton);
AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
if (extraDto != null) {
returnSearchPage(extraDto);
}
}
});
}
}
当然现在这样做模仿的依然不够彻底,在选择的时候,会发现,spinner会在用2根手指触碰2个选项的时候,只有一个背景色变化,其他选项颜色不变,
看上去就是自动不让你选中了。
而radiogroup是,2个选项会变色,但是只有一个选项被触发选中事件。当然这个只是表现上的区别,其实radiogroup也没错。
接下来就是尝试把radiobutton的样式定义在xml,比如一开始的那样,完了在程序中取得模板,并应用在radiobutton对象中。
做法二,代码如下:
private void initCityGroup(AddressMasterDto addressMasterDto) {
radioGroup.setVisibility(View.VISIBLE);
ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
lvDetailInfo.setVisibility(View.GONE);
resultDtoList = initCityList(addressMasterDto);
if (resultDtoList != null && resultDtoList.size() > 0) {
for (DtoBase dto : resultDtoList) {
AddressMasterDto addressDto = (AddressMasterDto) dto;
if (addressDto != null) {
RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.spinner_radiobutton, null);
radioButton.setText(addressDto.getText());
RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
radioGroup.addView(radioButton); // 这里是在选项间添加了一个分割线。这个做法很好。
if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
View v = new View(this);
v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
radioGroup.addView(v);
}
}
} // 这是为了选中添加监听事件
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
// 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
// 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
int index = group.indexOfChild(radioButton);
AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
if (extraDto != null) {
returnSearchPage(extraDto);
}
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:textColor="@android:color/black"
android:textSize="20sp" >
</RadioButton>
以上。
RaidoGroup+RadioButton模拟android下拉框弹出List的更多相关文章
- div模拟的下拉框特效
随笔- 4 文章- 0 评论- 0 ? <style type="text/css"> body, ul, li { margin: 0; padding: 0; fo ...
- div模拟的下拉框特效jquery
从网上找来的,感觉不错就拿来分享下 <style type="text/css"> body, ul, li { margin: 0; padding: 0; font ...
- android下拉框
XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...
- 用div,ul,input模拟select下拉框
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- jq插件又来了,模拟select下拉框,支持上下方向键哦
好久没来了,更新下插件, 这个原理就是利用的 input[type='hidden']和自定义属性data-value捆绑传值操作的,可是设置默认选项,回调等参数,代码不多,比较简单,吼吼 (func ...
- jquery实现模拟select下拉框效果
<IGNORE_JS_OP style="WORD-WRAP: break-word"> <!DOCTYPE html PUBLIC "-//W3C// ...
- ul -- li 模拟select下拉框
在写项目中 用到下拉框,一般用 <select name="" id=""> <option value=</option> &l ...
- css+html+js实现多级下拉和弹出菜单
本文将使用css+html+js实现横向菜单.具有多级弹出菜单下拉. 首先我们来看看效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvajkwMzgy ...
- 模拟select下拉框、复选框效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
随机推荐
- Section 1.4 The Clocks
0 0 虽然不知不觉做到了Section 1.4了,但是都没有把做题的想法和代码发到这里… 本来今天想从Section 1.2补起来然后发现之前做的题都忘了…(Name That Number那道题是 ...
- Map学习
1.Query Operations(查询操作) int size();boolean isEmpty(); boolean containsKey(Object key);boolean conta ...
- [转]shell基本算术运算
from:http://www.cnblogs.com/yfanqiu/archive/2012/05/10/2494031.html#undefined shell程序中的操作默认都是字符串操作,在 ...
- RPI学习--WebCam_mplayer
1,安装mplayer $ sudo apt-get install mplayer 2,运行 $ sudo mplayer tv:// 有时会秀逗,绿屏,多试几下就好了,情况未知
- 后台获取不规则排列RadioButton组的值
获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList: <asp:RadioButtonList ID="rbl" runat=&quo ...
- C# CsvFile 类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- IntelliJ IDEA 12.0
User name:JavaDeveloper Serial number:92547-KY2BB-QZ0S1-PEZCV-HUT8Q-6RYY4
- xcode插件种类
古人云“工欲善其事必先利其器”,打造一个强大的开发环境,是立即提升自身战斗力的绝佳途径!以下是搜集的一些有力的XCode插件. 1.全能搜索家CodePilot 2.0 你要找的是文件?是文件夹? ...
- DP重新学
白书上的DP讲义:一 二 DAG上的dp 不要好高骛远去学这种高端东西,学了也写不对,剩下的几天把基本的dp和搜索搞下,就圆满了.不要再学新算法了,去九度把现有的算法写个痛. 学了数位DP和记忆搜索, ...
- BZOJ 1045 糖果传递
奇怪的式子.最后发现取中位数. #include<iostream> #include<cstdio> #include<cstring> #include< ...