Android-自定义View之 可换行的radioGroup 和 字母索引

            }

        }

    }

    setMeasuredDimension(maxWidth,y);//设置容器需要的宽度和高度

}



@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

    int maxWidth=r-1;//获取最大宽度

    int x = 0,y = 0,row = 0;

    int maxHeight = 0;

    for(int index = 0;index < getChildCount();index++){

        View child=getChildAt(index);

        if(child.getVisibility()!=GONE){

            int width = child.getMeasuredWidth(),height = child.getMeasuredHeight();

            if(height>maxHeight)

                maxHeight = height;

            x+=width+marginLeft;

            if(row<1){

                y=maxHeight+marginTop;

            }

            if(x>maxWidth){

                if(index!=0)

                    row++;

                if(width>=maxWidth-marginLeft){

                    x=maxWidth;

                }else {

                    x=width+marginLeft;

                }

                y+=maxHeight+marginTop;

                maxHeight=0;

            }

            child.layout(x-width,y-height,x,y);

        }

    }





}



private void init() {

    mOnCheckedChangeListener=new CompoundButton.OnCheckedChangeListener() {

        @Override

        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if(b){

                checkedList.add(compoundButton.getId());

            }else {

                checkedList.remove(compoundButton.getId());

            }

        }

    };

}





@Override

public void addView(View child) {

    if(child instanceof CheckBox){

        CheckBox checkBox = (CheckBox) child;

        if(checkBox.isChecked()){

            setCheckIdList(checkBox.getId());

        }

        checkBox.setOnCheckedChangeListener(mOnCheckedChangeListener);

    }

    super.addView(child);

}



private void setCheckIdList(@IdRes int id) {

    checkedList.add(id);

}



public void clearChecked(){

    checkedList.clear();

}

private void removeChecked(@IdRes int id){

    checkedList.remove(id);

}

}




attr.xml文件:



<declare-styleable name="CheckBoxGroup">

    <attr name="childMarginLeft" format="dimension"/>

    <attr name="childMarginTop" format="dimension"/>

</declare-styleable>

public class ScreenCheckBox extends CheckBox {

public ScreenCheckBox(Context context) {

    super(context);

    setTextStyle();

    setOnCheckChangeLiistener();

}

private void setOnCheckChangeLiistener(){

    this.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override

        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if (b) {

                compoundButton.setText("√" + compoundButton.getText());

                compoundButton.setTextColor(Color.parseColor("#d52c34"));

            }else if (compoundButton.getText().toString().contains("√")){

                compoundButton.setText(getText().subSequence(1, compoundButton.getText().length()));

                compoundButton.setTextColor(Color.parseColor("#000000"));

            }

        }

    });

}

private void setTextStyle(){

    setBackgroundResource(R.drawable.bg_checkbox);

    setButtonTintMode(PorterDuff.Mode.CLEAR);

    if (isChecked()) {

        setText("√" + getText());

        setTextColor(Color.parseColor("#d52c34"));

    }

}

public String getCheckBoxText(){

    if (getText().toString().contains("√")){

        return getText().toString().substring(1);

    }else

        return getText().toString();

}

}




drawable文件下新建3个xml文件bg\_checkbox,bg\_check\_red,bg\_check\_gray:



bg_checkbox:

<item android:drawable="@drawable/bg_check_red" android:state_checked="true"/>

<item android:drawable="@drawable/bg_check_gray" android:state_checked="false"/>

bg_check_red:

<corners android:radius="3dp" />

<stroke

    android:width="1dp"

    android:color="#d52c3d" />



`接下来是字母索引:`



`自定义View  继承View,重写onDraw方法`



protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);

    // 获取焦点改变背景颜色.

    int height = getHeight();// 获取对应高度

    int width = getWidth(); // 获取对应宽度

    int singleHeight = height / b.length;// 获取每一个字母的高度



    for (int i = 0; i < b.length; i++) {

        paint.setColor(Color.rgb(33, 65, 98));

        // paint.setColor(Color.WHITE);

        paint.setTypeface(Typeface.DEFAULT_BOLD);

        paint.setAntiAlias(true);

        paint.setTextSize(30);

        // 选中的状态

        if (i == choose) {

// paint.setColor(Color.parseColor(“#3399ff”));

            paint.setColor(getResources().getColor(R.color.maincolor));

            paint.setFakeBoldText(true);

        }

        // x坐标等于中间-字符串宽度的一半.

        float xPos = width / 2 - paint.measureText(b[i]) / 2;

        float yPos = singleHeight * i + singleHeight;

        canvas.drawText(b[i], xPos, yPos, paint);

        paint.reset();// 重置画笔

    }



}

public boolean dispatchTouchEvent(MotionEvent event) {

    final int action = event.getAction();

    final float y = event.getY();// 点击y坐标

    final int oldChoose = choose;

    final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;

    final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.



    switch (action) {

        case MotionEvent.ACTION_UP:

            setBackgroundDrawable(new ColorDrawable(0x00000000));

            choose = -1;//

            invalidate();

            if (mTextDialog != null) {

                mTextDialog.setVisibility(View.INVISIBLE);

            }

            break;



        default:

// setBackgroundResource(R.drawable.sidebar_background);

            if (oldChoose != c) {

                if (c >= 0 && c < b.length) {

                    if (listener != null) {

                        listener.onTouchingLetterChanged(b[c]);

                    }

                    if (mTextDialog != null) {

                        mTextDialog.setText(b[c]);

                        mTextDialog.setVisibility(View.VISIBLE);

                    }



                    choose = c;

                    invalidate();

                }

            }



            break;

    }

    return true;

}

/**

  • 向外公开的方法

  • @param onTouchingLetterChangedListener

*/

public void setOnTouchingLetterChangedListener(

    OnTouchingLetterChangedListener onTouchingLetterChangedListener) {

this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;

}

/**

  • 接口

  • @author coder

*/

public interface OnTouchingLetterChangedListener {

public void onTouchingLetterChanged(String s);

}




在activity页面:



//设置右侧触摸监听

sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

@Override
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值