zoukankan      html  css  js  c++  java
  • 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。

    做法一,代码如下:

     1     private void initCityGroup(AddressMasterDto addressMasterDto) {
     2         radioGroup.setVisibility(View.VISIBLE);
     3         ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
     4         lvDetailInfo.setVisibility(View.GONE);
     5         resultDtoList = initCityList(addressMasterDto);
     6         if (resultDtoList != null && resultDtoList.size() > 0) {
     7             for (DtoBase dto : resultDtoList) {
     8                 AddressMasterDto addressDto = (AddressMasterDto) dto;
     9                 if (addressDto != null) {
    10                     RadioButton radioButton = new RadioButton(this);
    11                     // 其实ID设不设置感觉问题不大,因为本身就不被关心。并且不建议这样设置,为了保证唯一性。
    12                     //radioButton.setId(resultDtoList.indexOf(dto) + 1);
    13                     radioButton.setText(addressDto.getText());
    14                     radioButton.setTextSize(20);
    15                     radioButton.setTextColor(getResources().getColor(R.color.black));// 文字颜色android默认是白色~~,我这边背景色是白的,于是我改成黑色的才显示文字。
    16 
    17                     // 下面这四行是让文字居左,按钮居右。
    18                     radioButton.setButtonDrawable(getResources().getDrawable(android.R.color.transparent));// 让原来左边的单选按钮不显示。
    19                     Drawable radioDrawable = getResources().getDrawable(android.R.drawable.btn_radio);
    20                     radioDrawable.setBounds(0, 0, radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());
    21                     radioButton.setCompoundDrawables(null, null, radioDrawable, null);
    22 
    23                     RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
    24                     radioLayout.setMargins(-35, 0, 0, 0); // *1 默认match_parent貌似左边并没有到边,剩下的距离自行调整,我这里就用了负数来设定marginLeft来达到靠左。
    25                     radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
    26                     radioGroup.addView(radioButton);
    27 
    28                     // 这里是在选项间添加了一个分割线。这个做法很好。
    29                     if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
    30                         View v = new View(this);
    31                         v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
    32                         v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
    33                         radioGroup.addView(v);
    34                     }
    35                 }
    36             }
    37 
    38             // 这是为了选中添加监听事件
    39             radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    40 
    41                 @Override
    42                 public void onCheckedChanged(RadioGroup group, int checkedId) {
    43                     // 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
    44                     // 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
    45                     // 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
    46                     RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
    47                     int index = group.indexOfChild(radioButton);
    48                     AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
    49                     if (extraDto != null) {
    50                         returnSearchPage(extraDto);
    51                     }
    52                 }
    53             });
    54         }
    55     }

    当然现在这样做模仿的依然不够彻底,在选择的时候,会发现,spinner会在用2根手指触碰2个选项的时候,只有一个背景色变化,其他选项颜色不变,

    看上去就是自动不让你选中了。

    而radiogroup是,2个选项会变色,但是只有一个选项被触发选中事件。当然这个只是表现上的区别,其实radiogroup也没错。

    接下来就是尝试把radiobutton的样式定义在xml,比如一开始的那样,完了在程序中取得模板,并应用在radiobutton对象中。

    做法二,代码如下:

     1 private void initCityGroup(AddressMasterDto addressMasterDto) {
     2         radioGroup.setVisibility(View.VISIBLE);
     3         ListView lvDetailInfo = (ListView) findViewById(R.id.lvDetailInfo);
     4         lvDetailInfo.setVisibility(View.GONE);
     5         resultDtoList = initCityList(addressMasterDto);
     6         if (resultDtoList != null && resultDtoList.size() > 0) {
     7             for (DtoBase dto : resultDtoList) {
     8                 AddressMasterDto addressDto = (AddressMasterDto) dto;
     9                 if (addressDto != null) {
    10                     RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.spinner_radiobutton, null);
    11                     radioButton.setText(addressDto.getText());
    12                     RadioGroup.LayoutParams radioLayout = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 60);
    13                     radioButton.setLayoutParams(radioLayout);// 使RadioButton的文字和按钮分别能够填满一行。效果就是文字和按钮向两边对齐。*1
    14                     radioGroup.addView(radioButton);
    15 
    16                     // 这里是在选项间添加了一个分割线。这个做法很好。
    17                     if (resultDtoList.size() - 1 != resultDtoList.indexOf(dto)) {
    18                         View v = new View(this);
    19                         v.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, 1));
    20                         v.setBackgroundColor(getResources().getColor(R.color.bg_gray));
    21                         radioGroup.addView(v);
    22                     }
    23                 }
    24             }
    25 
    26             // 这是为了选中添加监听事件
    27             radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    28 
    29                 @Override
    30                 public void onCheckedChanged(RadioGroup group, int checkedId) {
    31                     // 认为子选项的Id其实不重要的原因就在这里,动态设置的时候,为了和List能够配合的上,
    32                     // 只要取得index就可以了,很多人用checkId去和具体的RadioButton的Id来进行Switch,
    33                     // 感觉动态的时候没有必要如此,毕竟子选项的Id没有任何意义。
    34                     RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
    35                     int index = group.indexOfChild(radioButton);
    36                     AddressMasterDto extraDto = (AddressMasterDto) resultDtoList.get(index / 2);
    37                     if (extraDto != null) {
    38                         returnSearchPage(extraDto);
    39                     }
    40                 }
    41             });
    42         }
    43     }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="wrap_content"
     5     android:background="@android:color/white"
     6     android:button="@null"
     7     android:drawableRight="@android:drawable/btn_radio"
     8     android:textColor="@android:color/black"
     9     android:textSize="20sp" >
    10 </RadioButton>

    以上。

  • 相关阅读:
    谈谈Oracle基本操作(下)
    谈谈Oracle基本操作(上)
    java理论之java多线程与网络编程
    java理论之java--GUI(图形用户管理)与 IO/流
    java理论之java数组和集合
    存图片的
    .html()渲染后的内容还是带标签的字符串的问题
    Vuejs报错error: Unexpected console statement (no-console) at src... 解决办法
    常用正则表达式
    移动WEB---01.关于屏幕像素
  • 原文地址:https://www.cnblogs.com/niutouzdq/p/4220775.html
Copyright © 2011-2022 走看看