RaidoGroup+RadioButton模拟android下拉框弹出List

时间:2023-03-08 15:58:17
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>

以上。