栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android自定义View之RadioGroup实现跨多行显示

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android自定义View之RadioGroup实现跨多行显示

本文实例为大家分享了Android RadioGroup跨多行显示的具体代码,供大家参考,具体内容如下

此自定义View源于网络,具体出处不详。

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

public class RadioGroup extends LinearLayout {
 // holds the checked id; the selection is empty by default 
 private int mCheckedId = -1;
 // tracks children radio buttons checked state 
 private CompoundButton.onCheckedChangeListener mChildOnCheckedChangeListener;
 // when true, monCheckedChangeListener discards events 
 private boolean mProtectFromCheckedChange = false;
 private onCheckedChangeListener mOnCheckedChangeListener;
 private PassThroughHierarchyChangeListener mPassThroughListener;

 
 public RadioGroup(Context context) {
  super(context);
  setOrientation(VERTICAL);
  init();
 }

 
 public RadioGroup(Context context, AttributeSet attrs) {
  super(context, attrs);
  mCheckedId = View.NO_ID;

  final int index = VERTICAL;
  setOrientation(index);

  init();
 }

 private void init() {
  mChildonCheckedChangeListener = new CheckedStateTracker();
  mPassThroughListener = new PassThroughHierarchyChangeListener();
  super.setonHierarchyChangeListener(mPassThroughListener);
 }

 
 @Override
 public void setonHierarchyChangeListener(onHierarchyChangeListener listener) {
  // the user listener is delegated to our pass-through listener 
  mPassThroughListener.monHierarchyChangeListener = listener;
 }

 
 @Override
 protected void onFinishInflate() {
  super.onFinishInflate();

  // checks the appropriate radio button as requested in the XML file 
  if (mCheckedId != -1) {
   mProtectFromCheckedChange = true;
   setCheckedStateForView(mCheckedId, true);
   mProtectFromCheckedChange = false;
   setCheckedId(mCheckedId);
  }
 }

 @Override
 public void addView(final View child, int index, ViewGroup.LayoutParams params) {
  if (child instanceof RadioButton) {
   child.setonTouchListener(new onTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
     ((RadioButton) child).setChecked(true);
     checkRadioButton((RadioButton) child);
     if (monCheckedChangeListener != null) {
      mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, child.getId());
     }
     return true;
    }
   });
  } else if (child instanceof LinearLayout) {
   int childCount = ((LinearLayout) child).getChildCount();
   for (int i = 0; i < childCount; i++) {
    View view = ((LinearLayout) child).getChildAt(i);
    if (view instanceof RadioButton) {
     final RadioButton button = (RadioButton) view;
     button.setonTouchListener(new onTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
button.setChecked(true);
checkRadioButton(button);
if (monCheckedChangeListener != null) {
 mOnCheckedChangeListener.onCheckedChanged(RadioGroup.this, button.getId());
}
return true;
      }
     });
    }
   }
  }
  super.addView(child, index, params);
 }

 private void checkRadioButton(RadioButton radioButton) {
  View child;
  int radioCount = getChildCount();
  for (int i = 0; i < radioCount; i++) {
   child = getChildAt(i);
   if (child instanceof RadioButton) {
    if (child == radioButton) {
     // do nothing 
    } else {
     ((RadioButton) child).setChecked(false);
    }
   } else if (child instanceof LinearLayout) {
    int childCount = ((LinearLayout) child).getChildCount();
    for (int j = 0; j < childCount; j++) {
     View view = ((LinearLayout) child).getChildAt(j);
     if (view instanceof RadioButton) {
      final RadioButton button = (RadioButton) view;
      if (button == radioButton) {
// do nothing 
      } else {
button.setChecked(false);
      }
     }
    }
   }
  }
 }

 
 public void check(int id) {
  // don't even bother 
  if (id != -1 && (id == mCheckedId)) {
   return;
  }

  if (mCheckedId != -1) {
   setCheckedStateForView(mCheckedId, false);
  }

  if (id != -1) {
   setCheckedStateForView(id, true);
  }

  setCheckedId(id);
 }

 private void setCheckedId(int id) {
  mCheckedId = id;
 }

 private void setCheckedStateForView(int viewId, boolean checked) {
  View checkedView = findViewById(viewId);
  if (checkedView != null && checkedView instanceof RadioButton) {
   ((RadioButton) checkedView).setChecked(checked);
  }
 }

 
 public int getCheckedRadioButtonId() {
  return mCheckedId;
 }

 
 public void clearCheck() {
  check(-1);
 }

 
 public void setonCheckedChangeListener(onCheckedChangeListener listener) {
  monCheckedChangeListener = listener;
 }

 
 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new RadioGroup.LayoutParams(getContext(), attrs);
 }

 
 @Override
 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
  return p instanceof RadioGroup.LayoutParams;
 }

 @Override
 protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
  return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
 }

 @Override
 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  super.onInitializeAccessibilityEvent(event);
  event.setClassName(RadioGroup.class.getName());
 }

 @Override
 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  super.onInitializeAccessibilityNodeInfo(info);
  info.setClassName(RadioGroup.class.getName());
 }

 public static class LayoutParams extends LinearLayout.LayoutParams {
  
  public LayoutParams(Context c, AttributeSet attrs) {
   super(c, attrs);
  }

  
  public LayoutParams(int w, int h) {
   super(w, h);
  }

  
  public LayoutParams(int w, int h, float initWeight) {
   super(w, h, initWeight);
  }

  
  public LayoutParams(ViewGroup.LayoutParams p) {
   super(p);
  }

  
  public LayoutParams(MarginLayoutParams source) {
   super(source);
  }

  
  @Override
  protected void setbaseAttributes(TypedArray a, int widthAttr, int heightAttr) {
   if (a.hasValue(widthAttr)) {
    width = a.getLayoutDimension(widthAttr, "layout_width");
   } else {
    width = WRAP_CONTENT;
   }

   if (a.hasValue(heightAttr)) {
    height = a.getLayoutDimension(heightAttr, "layout_height");
   } else {
    height = WRAP_CONTENT;
   }
  }
 }

 
 public interface onCheckedChangeListener {
  
  void onCheckedChanged(RadioGroup group, int checkedId);
 }

 private class CheckedStateTracker implements CompoundButton.onCheckedChangeListener {
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   // prevents from infinite recursion 
   if (mProtectFromCheckedChange) {
    return;
   }

   mProtectFromCheckedChange = true;
   if (mCheckedId != -1) {
    setCheckedStateForView(mCheckedId, false);
   }
   mProtectFromCheckedChange = false;

   int id = buttonView.getId();
   setCheckedId(id);
  }
 }

 
 private class PassThroughHierarchyChangeListener implements ViewGroup.onHierarchyChangeListener {
  private ViewGroup.onHierarchyChangeListener mOnHierarchyChangeListener;

  
  public void onChildViewAdded(View parent, View child) {
   if (parent == RadioGroup.this && child instanceof RadioButton) {
    int id = child.getId();
    // generates an id if it's missing 
    if (id == View.NO_ID) {
     id = child.hashCode();
     child.setId(id);
    }
    ((RadioButton) child).setonCheckedChangeListener(mChildOnCheckedChangeListener);
   }

   if (monHierarchyChangeListener != null) {
    mOnHierarchyChangeListener.onChildViewAdded(parent, child);
   }
  }

  
  public void onChildViewRemoved(View parent, View child) {
   if (parent == RadioGroup.this && child instanceof RadioButton) {
    ((RadioButton) child).setonCheckedChangeListener(null);
   }

   if (monHierarchyChangeListener != null) {
    mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
   }
  }
 }
}

使用:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/154647.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号