Commit 13aef8bd authored by 赵鹏翔's avatar 赵鹏翔

修改自定义数字键盘,kotlin化处理

parent d6e641b0
......@@ -9,8 +9,9 @@ import androidx.lifecycle.ViewModelProvider
import com.fastcashier.lib_common.base.BaseActivity
import com.fastcashier.lib_common.config.IntentExtra
import com.fastcashier.lib_common.ui.dialog.CommonDialog
import com.fastcashier.lib_common.ui.widget.DecimalKeyboard.DELETE
import com.fastcashier.lib_common.ui.widget.DecimalKeyboard.DOT
import com.fastcashier.lib_common.ui.widget.DecimalKeyboard
import com.fastcashier.lib_common.ui.widget.DecimalKeyboard.Companion.DELETE
import com.fastcashier.lib_common.ui.widget.DecimalKeyboard.Companion.DOT
import com.fastcashier.lib_common.util.StringPriceFormatUtils
import com.fastcashier.lib_common.util.clickWithTrigger
import com.miya.fastcashier.R
......@@ -115,66 +116,68 @@ class PriceInputActivity : BaseActivity() {
binding.llBack.clickWithTrigger { finish() }
binding.numKeyboard.setKeyboardClickListener {
when (it) {
DELETE -> {
if (TextUtils.isEmpty(binding.etPrice.text.toString())) {
return@setKeyboardClickListener
}
var currentCode: String = binding.etPrice.text.toString()
currentCode = if (currentCode.length <= 1) {
""
} else {
currentCode.substring(0, currentCode.length - 1)
}
binding.etPrice.setText(currentCode)
if (binding.tvInvalidRefundPrice.visibility == View.VISIBLE) {
binding.tvInvalidRefundPrice.visibility = View.GONE
}
}
DOT -> {
var currentCode: String = binding.etPrice.text.toString()
if (!currentCode.contains(".") && !TextUtils.isEmpty(currentCode)) {
currentCode += "."
binding.numKeyboard.setKeyDecimalKeyboardClickListener(object :DecimalKeyboard.KeyboardClickListener{
override fun onNumClick(it: Int) {
when (it) {
DELETE -> {
if (TextUtils.isEmpty(binding.etPrice.text.toString())) {
return
}
var currentCode: String = binding.etPrice.text.toString()
currentCode = if (currentCode.length <= 1) {
""
} else {
currentCode.substring(0, currentCode.length - 1)
}
binding.etPrice.setText(currentCode)
} else {
CenterToasty.error(this, "金额格式有误!", Toast.LENGTH_SHORT).show()
if (binding.tvInvalidRefundPrice.visibility == View.VISIBLE) {
binding.tvInvalidRefundPrice.visibility = View.GONE
}
}
}
else -> {
var currentCode: String = binding.etPrice.text.toString()
if (currentCode.contains(".")) {
val tmp =
currentCode.substring(
currentCode.indexOf("."),
currentCode.length - 1
)
if (tmp.length < 2) {
if (BigDecimal(currentCode + it).compareTo(BigDecimal.ZERO) == 0 && tmp.length == 1) {
CenterToasty.error(this, "金额不能为0!", Toast.LENGTH_SHORT).show()
} else {
currentCode += it
binding.etPrice.text = currentCode
}
DOT -> {
var currentCode: String = binding.etPrice.text.toString()
if (!currentCode.contains(".") && !TextUtils.isEmpty(currentCode)) {
currentCode += "."
binding.etPrice.setText(currentCode)
} else {
CenterToasty.error(this, "金额最多设置2位小数!", Toast.LENGTH_SHORT).show()
CenterToasty.error(this@PriceInputActivity, "金额格式有误!", Toast.LENGTH_SHORT).show()
}
} else {
if (currentCode.startsWith("0") && currentCode.length == 1 && it == 0) {
CenterToasty.error(this, "第二位不能输入0!", Toast.LENGTH_SHORT).show()
}
else -> {
var currentCode: String = binding.etPrice.text.toString()
if (currentCode.contains(".")) {
val tmp =
currentCode.substring(
currentCode.indexOf("."),
currentCode.length - 1
)
if (tmp.length < 2) {
if (BigDecimal(currentCode + it).compareTo(BigDecimal.ZERO) == 0 && tmp.length == 1) {
CenterToasty.error(this@PriceInputActivity, "金额不能为0!", Toast.LENGTH_SHORT).show()
} else {
currentCode += it
binding.etPrice.text = currentCode
}
} else {
CenterToasty.error(this@PriceInputActivity, "金额最多设置2位小数!", Toast.LENGTH_SHORT).show()
}
} else {
if (BigDecimal(currentCode + it).compareTo(BigDecimal("99999")) == 1) {
CenterToasty.error(this, "金额不能超过99999!", Toast.LENGTH_SHORT)
.show()
if (currentCode.startsWith("0") && currentCode.length == 1 && it == 0) {
CenterToasty.error(this@PriceInputActivity, "第二位不能输入0!", Toast.LENGTH_SHORT).show()
} else {
currentCode += it
binding.etPrice.text = currentCode
if (BigDecimal(currentCode + it).compareTo(BigDecimal("99999")) == 1) {
CenterToasty.error(this@PriceInputActivity, "金额不能超过99999!", Toast.LENGTH_SHORT)
.show()
} else {
currentCode += it
binding.etPrice.text = currentCode
}
}
}
}
}
}
}
})
viewModel.payServiceResponseLiveData.observe(this, { result ->
dismissProgressDialog()
......
package com.fastcashier.lib_common.ui.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.fastcashier.lib_common.R;
import java.util.ArrayList;
import java.util.List;
public class DecimalKeyboard extends LinearLayout {
public static final int DELETE = -2;
public static final int DOT = -3;
private View btn_7, btn_8, btn_9, btn_6, btn_5, btn_4, btn_3, btn_2, btn_1, btn_0;
private View delete;
private Button btn_dot;
private List<View> buttons;
private KeyboardClickListener keyboardClickListener;
public DecimalKeyboard(Context context) {
this(context, null);
}
public DecimalKeyboard(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.app_layout_keyboard_decimal, this, true);
buttons = new ArrayList<>(12);
ClickListener clickListener = new ClickListener();
btn_7 = view.findViewById(R.id.btn_7);
btn_8 = view.findViewById(R.id.btn_8);
btn_9 = view.findViewById(R.id.btn_9);
btn_6 = view.findViewById(R.id.btn_6);
btn_5 = view.findViewById(R.id.btn_5);
btn_4 = view.findViewById(R.id.btn_4);
btn_3 = view.findViewById(R.id.btn_3);
btn_2 = view.findViewById(R.id.btn_2);
btn_1 = view.findViewById(R.id.btn_1);
btn_0 = view.findViewById(R.id.btn_0);
delete = view.findViewById(R.id.btn_delete);
btn_dot = view.findViewById(R.id.btn_dot);
buttons.add(btn_7);
buttons.add(btn_8);
buttons.add(btn_9);
buttons.add(btn_6);
buttons.add(btn_5);
buttons.add(btn_4);
buttons.add(btn_3);
buttons.add(btn_2);
buttons.add(btn_1);
buttons.add(btn_0);
buttons.add(delete);
buttons.add(btn_dot);
for (View btn : buttons) {
btn.setOnClickListener(clickListener);
}
}
class ClickListener implements OnClickListener {
@Override
public void onClick(View v) {
if (keyboardClickListener == null) {
return;
}
int i = v.getId();
if (i == R.id.btn_7) {
keyboardClickListener.onNumClick(7);
}
if (i == R.id.btn_8) {
keyboardClickListener.onNumClick(8);
}
if (i == R.id.btn_9) {
keyboardClickListener.onNumClick(9);
}
if (i == R.id.btn_6) {
keyboardClickListener.onNumClick(6);
}
if (i == R.id.btn_5) {
keyboardClickListener.onNumClick(5);
}
if (i == R.id.btn_4) {
keyboardClickListener.onNumClick(4);
}
if (i == R.id.btn_3) {
keyboardClickListener.onNumClick(3);
}
if (i == R.id.btn_2) {
keyboardClickListener.onNumClick(2);
}
if (i == R.id.btn_1) {
keyboardClickListener.onNumClick(1);
}
if (i == R.id.btn_0) {
keyboardClickListener.onNumClick(0);
}
if (i == R.id.btn_delete) {
keyboardClickListener.onNumClick(DELETE);
}
if (i == R.id.btn_dot) {
keyboardClickListener.onNumClick(DOT);
}
}
}
public KeyboardClickListener getKeyboardClickListener() {
return keyboardClickListener;
}
public void setKeyboardClickListener(KeyboardClickListener keyboardClickListener) {
this.keyboardClickListener = keyboardClickListener;
}
public interface KeyboardClickListener {
void onNumClick(int num);
}
}
package com.fastcashier.lib_common.ui.widget
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import com.fastcashier.lib_common.R
import java.util.*
class DecimalKeyboard @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
private val btn_7: View
private val btn_8: View
private val btn_9: View
private val btn_6: View
private val btn_5: View
private val btn_4: View
private val btn_3: View
private val btn_2: View
private val btn_1: View
private val btn_0: View
private val delete: View
private val btn_dot: Button
private val buttons: MutableList<View>
var keyboardClickListener: KeyboardClickListener? = null
internal inner class ClickListener : OnClickListener {
override fun onClick(v: View) {
if (keyboardClickListener == null) {
return
}
val i = v.id
if (i == R.id.btn_7) {
keyboardClickListener!!.onNumClick(7)
}
if (i == R.id.btn_8) {
keyboardClickListener!!.onNumClick(8)
}
if (i == R.id.btn_9) {
keyboardClickListener!!.onNumClick(9)
}
if (i == R.id.btn_6) {
keyboardClickListener!!.onNumClick(6)
}
if (i == R.id.btn_5) {
keyboardClickListener!!.onNumClick(5)
}
if (i == R.id.btn_4) {
keyboardClickListener!!.onNumClick(4)
}
if (i == R.id.btn_3) {
keyboardClickListener!!.onNumClick(3)
}
if (i == R.id.btn_2) {
keyboardClickListener!!.onNumClick(2)
}
if (i == R.id.btn_1) {
keyboardClickListener!!.onNumClick(1)
}
if (i == R.id.btn_0) {
keyboardClickListener!!.onNumClick(0)
}
if (i == R.id.btn_delete) {
keyboardClickListener!!.onNumClick(DELETE)
}
if (i == R.id.btn_dot) {
keyboardClickListener!!.onNumClick(DOT)
}
}
}
interface KeyboardClickListener {
fun onNumClick(num: Int)
}
companion object {
const val DELETE = -2
const val DOT = -3
}
init {
val view =
LayoutInflater.from(context).inflate(R.layout.app_layout_keyboard_decimal, this, true)
buttons = ArrayList(12)
val clickListener: ClickListener = ClickListener()
btn_7 = view.findViewById(R.id.btn_7)
btn_8 = view.findViewById(R.id.btn_8)
btn_9 = view.findViewById(R.id.btn_9)
btn_6 = view.findViewById(R.id.btn_6)
btn_5 = view.findViewById(R.id.btn_5)
btn_4 = view.findViewById(R.id.btn_4)
btn_3 = view.findViewById(R.id.btn_3)
btn_2 = view.findViewById(R.id.btn_2)
btn_1 = view.findViewById(R.id.btn_1)
btn_0 = view.findViewById(R.id.btn_0)
delete = view.findViewById(R.id.btn_delete)
btn_dot = view.findViewById(R.id.btn_dot)
buttons.add(btn_7)
buttons.add(btn_8)
buttons.add(btn_9)
buttons.add(btn_6)
buttons.add(btn_5)
buttons.add(btn_4)
buttons.add(btn_3)
buttons.add(btn_2)
buttons.add(btn_1)
buttons.add(btn_0)
buttons.add(delete)
buttons.add(btn_dot)
for (btn in buttons) {
btn.setOnClickListener(clickListener)
}
}
fun setKeyDecimalKeyboardClickListener(keyboardClickListener: KeyboardClickListener){
this.keyboardClickListener = keyboardClickListener
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment