From 8a1ffa8969fd52dca697adbe912b79faa767d84c Mon Sep 17 00:00:00 2001 From: Raymond Yang Date: Thu, 3 Aug 2023 12:03:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=95=B8=E5=80=BC=E8=BC=B8?= =?UTF-8?q?=E5=85=A5UI=E5=85=83=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../floatingwindow/view/ValueEditor.kt | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 app/src/main/java/com/ray650128/floatingwindow/view/ValueEditor.kt diff --git a/app/src/main/java/com/ray650128/floatingwindow/view/ValueEditor.kt b/app/src/main/java/com/ray650128/floatingwindow/view/ValueEditor.kt new file mode 100644 index 0000000..4bb2547 --- /dev/null +++ b/app/src/main/java/com/ray650128/floatingwindow/view/ValueEditor.kt @@ -0,0 +1,114 @@ +package com.ray650128.floatingwindow.view + +import android.content.Context +import android.graphics.Color +import android.text.InputType +import android.util.AttributeSet +import android.widget.Button +import android.widget.EditText +import android.widget.ImageButton +import android.widget.LinearLayout +import androidx.appcompat.app.AlertDialog +import com.ray650128.floatingwindow.R +import com.ray650128.floatingwindow.dp +import com.ray650128.floatingwindow.px + +class ValueEditor: LinearLayout { + + constructor(context: Context): super(context) { + this.context = context + initContentView() + } + + constructor(context: Context, attrs: AttributeSet): super(context, attrs) { + this.context = context + initContentView() + } + + constructor(context: Context, attrs: AttributeSet, intRes: Int): super(context, attrs, intRes) { + this.context = context + initContentView() + } + + private lateinit var context: Context + + private var onValueChangeListener: OnValueChangeListener? = null + + private val btnAdd by lazy { + ImageButton(context).apply { + setImageResource(R.drawable.ic_value_add) + setBackgroundColor(Color.TRANSPARENT) + layoutParams = LayoutParams(48.dp, 48.dp) + } + } + + private val btnValue by lazy { + Button(context).apply { + text = "$value" + setBackgroundColor(Color.TRANSPARENT) + layoutParams = LayoutParams(0, 48.dp).apply { + weight = 1.0f + } + } + } + + private val btnMinus by lazy { + ImageButton(context).apply { + setImageResource(R.drawable.ic_value_minus) + setBackgroundColor(Color.TRANSPARENT) + layoutParams = LayoutParams(48.dp, 48.dp) + } + } + + var value: Int = 0 + set(value) { + field = value + btnValue.text = "$value" + } + + private fun initContentView() { + this.orientation = HORIZONTAL + + this.addView(btnAdd) + this.addView(btnValue) + this.addView(btnMinus) + + btnAdd.setOnClickListener { + onValueChangeListener?.onValueAdd() + } + + btnValue.setOnClickListener { + AlertDialog.Builder(context).apply { + val editText = EditText(context).apply { + inputType = InputType.TYPE_CLASS_NUMBER + } + setTitle("請輸入想要的值") + setView(editText) + setPositiveButton("確定") { dialog, _ -> + val inputText = editText.text?.toString()?.toIntOrNull() + onValueChangeListener?.onValueClick(inputText) + dialog.dismiss() + } + setNegativeButton("取消") { dialog, _ -> + dialog.dismiss() + } + }.show() + } + + btnMinus.setOnClickListener { + onValueChangeListener?.onValueMinus() + } + } + + fun setOnValueChangeListener(listener: OnValueChangeListener) { + this.onValueChangeListener = listener + } + + interface OnValueChangeListener { + fun onValueAdd() + + fun onValueMinus() + + fun onValueClick(value: Int?) + } +} \ No newline at end of file