Android 自定義View實現任意佈局的RadioGroup效果

這篇文章主要介紹了Android 自定義View實現任意佈局的RadioGroup,需要的朋友可以參考下

前言

RadioGroup是繼承LinearLayout,只支持橫向或者豎向兩種佈局。所以在某些情況,比如多行多列布局,RadioGroup就並不適用 。

本篇文章通過繼承RelativeLayout實現自定義RadioGroup,實現RadioButton的任意佈局。效果圖如下:

在這裏插入圖片描述

代碼(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效果,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章