RatioButton
- 帶有大小單位轉(zhuǎn)換功能的數(shù)量修改控件锰蓬,常用于商品管理app沥阱。
- Quantity modification control with size unit conversion function, commonly used in commodity management app
GitHub地址: https://github.com/lintianlin/RatioButton
詳細(xì)介紹
- 我們開發(fā)過程中供炼,部分類型的商品管理app會(huì)經(jīng)常用到大小單位轉(zhuǎn)換崇摄,如酒類中的1箱=6瓶的轉(zhuǎn)換拉鹃。RatioButton就是將大小單位轉(zhuǎn)換的邏輯辈赋,庫(kù)存判斷邏輯,越界判斷邏輯進(jìn)行了封裝膏燕,避免了重復(fù)造輪子钥屈。目前的v1.0.0還是一個(gè)基礎(chǔ)版本,后期還需要進(jìn)行優(yōu)化坝辫。
示例效果
dialog.gif
ratio.gif
yj.gif
zero.gif
關(guān)鍵代碼
public void setDisplayCount(int tempSmallCount, int tempBigCount) {
if (ratio > 0) {//ratio為轉(zhuǎn)換率篷就,ratio>0是當(dāng)有大單位的時(shí)候
if (tempBigCount * ratio + tempSmallCount > maxCount) {
ToastUtils.showToast(context, ERRORTIP);
bigCount = maxCount / ratio;//取整運(yùn)算
smallCount = maxCount % ratio;//取余運(yùn)算
} else {
if (tempSmallCount > ratio) {//當(dāng)小單位數(shù)量大于ratio的時(shí)候需要進(jìn)位
smallCount = tempSmallCount % ratio;
bigCount = tempBigCount + tempSmallCount / ratio;
} else {
smallCount = tempSmallCount;
bigCount = tempBigCount;
}
}
} else {
smallCount = tempSmallCount;
bigCount = tempBigCount;
}
if (smallCount == 0) {//越界判斷,當(dāng)數(shù)量為0時(shí)阀溶,減按鈕不能點(diǎn)擊并置灰
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));
}
屬性
屬性 | 屬性名稱 | 類型 | 默認(rèn)值 |
---|---|---|---|
btnHeight | 控件的高度 | dimension | 0 |
btnWidth | 控件的高度 | dimension | 0 |
tvTextSize | 數(shù)量的字體大小 | dimension | 0 |
editable | 是否可以直接編輯 | boolean | false |
haveBigUnit | 是否有大單位 | boolean | false |
minAmount | 最小數(shù)量 | 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) {
}
});