RatioButton:一个带有大小单位转换功能的数量修改控件

2018-12-11  本文已影响7人  SinFeeLoo

RatioButton

GitHub地址: https://github.com/lintianlin/RatioButton

详细介绍

示例效果

dialog.gif ratio.gif yj.gif zero.gif

关键代码


    public void setDisplayCount(int tempSmallCount, int tempBigCount) {
        if (ratio > 0) {//ratio为转换率,ratio>0是当有大单位的时候
            if (tempBigCount * ratio + tempSmallCount > maxCount) {
                ToastUtils.showToast(context, ERRORTIP);
                bigCount = maxCount / ratio;//取整运算
                smallCount = maxCount % ratio;//取余运算
            } else {
                if (tempSmallCount > ratio) {//当小单位数量大于ratio的时候需要进位
                    smallCount = tempSmallCount % ratio;
                    bigCount = tempBigCount + tempSmallCount / ratio;
                } else {
                    smallCount = tempSmallCount;
                    bigCount = tempBigCount;
                }
            }
        } else {
            smallCount = tempSmallCount;
            bigCount = tempBigCount;
        }
        if (smallCount == 0) {//越界判断,当数量为0时,减按钮不能点击并置灰
            btnSmallSub.setEnabled(false);
        } else {
            btnSmallSub.setEnabled(true);
        }
        if (bigCount == 0) {
            btnBigSub.setEnabled(false);
        } else {
            btnBigSub.setEnabled(true);
        }
        tvSmallCount.setText(String.valueOf(smallCount));
        tvBigCount.setText(String.valueOf(bigCount));
    }

属性

属性 属性名称 类型 默认值
btnHeight 控件的高度 dimension 0
btnWidth 控件的高度 dimension 0
tvTextSize 数量的字体大小 dimension 0
editable 是否可以直接编辑 boolean false
haveBigUnit 是否有大单位 boolean false
minAmount 最小数量 integer 0
margin 小单位按钮距离大单位按钮的距离 dimension 10

使用方法


    allprojects {
            repositories {
                ...
                maven { url 'https://jitpack.io' }
            }
        }

    dependencies {
                implementation 'com.github.lintianlin:RatioButton:v1.0.0'
        }

    <com.sinfeeloo.ratiobutton.RatiolBtn
            android:id="@+id/bsb_goods"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_goods_name"
            android:layout_alignParentEnd="true"
            app:margin="5dp"
            app:btnHeight="30dp"
            app:btnWidth="120dp">
        </com.sinfeeloo.ratiobutton.RatiolBtn>
         ratioBtn.setRatio(item.getRatio());
            ratioBtn.setMaxCount(item.getStorage());
            ratioBtn.setUnit(item.getSmallUnit(), item.getBigUnit());
            ratioBtn.setDisplayCount(item.getSmallcount(), item.getBigCount());
            ratioBtn.setOnCountChangedLisener(new OnCountChangedListener() {
                @Override
                public void onCountChange(View view, int bigCount, int smallCount) {
                    
                }
            });
上一篇 下一篇

猜你喜欢

热点阅读