美文网首页
单选多选按钮

单选多选按钮

作者: 糖葫芦_倩倩 | 来源:发表于2018-04-27 11:49 被阅读14次
notify9.gif

项目中出现单选多选这种情况挺多的,所以整合成一个控件了,使用方便,可以设置单选,也可以设置多选,最主要的是采用了流式布局,会自动根据屏幕大小和内容进行自动折行。

  • xml中:
<app.myapplication.ButtonFlowLayout
        android:id="@+id/buttnLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
  • 设置单选:
mButtonFLowLayout = findViewById(R.id.buttnLayout);

        data.add("天气不错");
        data.add("咔嚓咔嚓咔嚓");
        data.add("多云");
        data.add("雷电交加");
        data.add("雨夹雪");

        mButtonFLowLayout.setDataList(data);
  • 设置多选
mButtonFLowLayout.setDataList(data,true);

默认不传是单选效果,默认选中第一项,也可以修改:
0 默认是第一项,例如设置默认选中第二项:

mButtonFLowLayout.setSingleSelectItem(1);
  • 设置选中监听
    单选
mButtonFLowLayout.setOnSingleChanedListener(new ButtonFlowLayout.OnSingleChanedListener() {
           @Override
            public void onSingleChaned(int index) {
               Toast.makeText(MainActivity.this,"当前选中:"+index,Toast.LENGTH_SHORT).show();

            }
        });

多选

mButtonFLowLayout.setOnMultifyChanedListenerr(new ButtonFlowLayout.OnMultifyChanedListener() {
            @Override
            public void onMultifyChaned(List<Integer> indexs) {
               for(int i=0;i<indexs.size();i++){
                   Log.i("xx","当前选中的集合是:"+indexs.get(i));
                }
            }
        });

方便快捷,代码传送门:https://github.com/QQabby/MultifyCheckButton 喜欢就给个start吧。

相关文章

网友评论

      本文标题:单选多选按钮

      本文链接:https://www.haomeiwen.com/subject/amxblftx.html