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

RatioButton

  • 带有大小单位转换功能的数量修改控件,常用于商品管理app。
  • Quantity modification control with size unit conversion function, commonly used in commodity management app

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

详细介绍

  • 我们开发过程中,部分类型的商品管理app会经常用到大小单位转换,如酒类中的1箱=6瓶的转换。RatioButton就是将大小单位转换的逻辑,库存判断逻辑,越界判断逻辑进行了封装,避免了重复造轮子。目前的v1.0.0还是一个基础版本,后期还需要进行优化。

示例效果

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

使用方法

  • 1.Add it in your root build.gradle at the end of repositories:

    allprojects {
            repositories {
                ...
                maven { url 'https://jitpack.io' }
            }
        }
  • 2.Add the dependency

    dependencies {
                implementation 'com.github.lintianlin:RatioButton:v1.0.0'
        }
  • 3.edit xml

    <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>
  • 4.edit java
         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) {
                    
                }
            });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,035评论 3 119
  • 有一次在一刻tlak上听到纪元老师的早起,印象非常深刻,就一直关注,老师的微信公众号。这次老师推出了,自律星100...
    毛巾浴巾毛毯枕巾阅读 1,638评论 1 2
  • 夜 安安静静 如何独处才会快乐 做自己喜欢的事情 喜欢什么呢?工作之余,我真的很宅。 宅在家里,宅在家里做什么呢?...
    努力红阅读 1,943评论 0 0
  • 文/Roy等风来 木叶下流出血一般的光明 和着渐涌的微波 和夏蝉的碎鸣 在温软无际的夜 擎着油亮透明的遮阳伞 戏弄...
    Roy等风来阅读 1,410评论 0 1
  • 七律·又是教师节 文/周国新 孤灯陪伴一年年,岁月轮回旧梦连。 桃李有情常问候,光阴无意却相牵。 桂花淡...
    舒心语文阅读 1,014评论 0 1