欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义View实现任意布局的RadioGroup效果

程序员文章站 2023-10-30 17:02:10
前言 radiogroup是继承linearlayout,只支持横向或者竖向两种布局。所以在某些情况,比如多行多列布局,radiogroup就并不适用 。 本篇文章通过...

前言

radiogroup是继承linearlayout,只支持横向或者竖向两种布局。所以在某些情况,比如多行多列布局,radiogroup就并不适用 。

本篇文章通过继承relativelayout实现自定义radiogroup,实现radiobutton的任意布局。效果图如下:

Android 自定义View实现任意布局的RadioGroup效果

代码(relativeradiogroup)

/**
 * author : blackhao
 * time : 2018/10/26 10:46
 * description : 自定义 radiogroup
 */
public class relativeradiogroup extends relativelayout implements compoundbutton.oncheckedchangelistener {
  private int checkid = -1;
  private compoundbutton.oncheckedchangelistener mchildoncheckedchangelistener;
  public relativeradiogroup(context context) {
    super(context);
  }
  public relativeradiogroup(context context, attributeset attrs) {
    super(context, attrs);
  }
  public relativeradiogroup(context context, attributeset attrs, int defstyleattr) {
    super(context, attrs, defstyleattr);
  }
  @override
  protected void onlayout(boolean changed, int l, int t, int r, int b) {
    super.onlayout(changed, l, t, r, b);
    //添加监听
    for (int i = 0; i < getchildcount(); i++) {
      view v = getchildat(i);
      if (v instanceof radiobutton && !(v instanceof compoundbutton.oncheckedchangelistener)) {
        ((radiobutton) v).setoncheckedchangelistener(this);
      }
    }
  }
  @override
  public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
    setcheck(buttonview.getid(), ischecked);
  }
  public void check(@idres int checkid) {
    if (checkid == -1 || this.checkid == checkid) {
      return;
    }
    setcheck(checkid, true);
  }
  public void clearcheck() {
    setcheck(-1, false);
  }
  public int getcheckedradiobuttonid() {
    return this.checkid;
  }
  /**
   * 设置选中状态
   */
  private void setcheck(@idres int checkid, boolean ischecked) {
    if (checkid != -1 && this.checkid == checkid) {
      return;
    }
    if (checkid != -1) {
      compoundbutton view = (compoundbutton) findviewbyid(checkid);
      //未选中的radiobutton被选中
      if (checkid != this.checkid && ischecked) {
        this.checkid = checkid;
        if (mchildoncheckedchangelistener != null) {
          mchildoncheckedchangelistener.oncheckedchanged(view, true);
        }
        //某个radiobutton被选中,将其他的改为未选中
        for (int i = 0; i < getchildcount(); i++) {
          view v = getchildat(i);
          if (v instanceof radiobutton && v.getid() != checkid) {
            ((radiobutton) v).setchecked(false);
          } else if (v instanceof radiobutton && v.getid() == checkid) {
            ((radiobutton) v).setchecked(true);
          }
        }
      }
      //被选中的radiobutton被取消选中
      if (checkid == this.checkid && !ischecked) {
        this.checkid = checkid;
        if (mchildoncheckedchangelistener != null) {
          mchildoncheckedchangelistener.oncheckedchanged(view, false);
        }
      }
    } else {
      //清空所有选择
      if (this.checkid != -1) {
        this.checkid = -1;
        compoundbutton view = (compoundbutton) findviewbyid(this.checkid);
        //将选中的置为未选中
        if (view instanceof radiobutton) {
          view.setchecked(false);
        }
      }
    }
  }
  public void setoncheckedchangelistener(compoundbutton.oncheckedchangelistener mchildoncheckedchangelistener) {
    this.mchildoncheckedchangelistener = mchildoncheckedchangelistener;
  }
}

代码并没有太多,也很容易理解。有什么不明白的可以留言。

1、下载地址 : https://github.com/luochen-hao/blackhaocustomview

总结

以上所述是小编给大家介绍的android 自定义view实现任意布局的radiogroup效果,希望对大家有所帮助