【已解决】 自定义控件,无法获取到xml中的自定义属性的值,只能获取到默认值
package com.ttyh.customview.animation
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.RelativeLayout
import com.ttyh.customview.R
/**
* @author Create by RUOQ
* @description:
* @create_date :2022/4/1-10:49
* @Modified_time : 2022/4/1-10:49
*/
class InputNumberView : RelativeLayout {
private var mCurrentNumber:Int = 0
private lateinit var mBtnMinus:Button
private lateinit var mBtnPlus:Button //控件延迟初始化
private lateinit var mEtv:EditText
var mMin = 0
var mMax = 30
var mStep = 1
private var mDisable = false
private var mBtnColor = -1
private var mDefaultValue = 0
var onNumberChangeListener: ((Int) -> Unit)? = null //通过高阶函数的方法暴露接口,更新数值的变化
constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
//初始化属性
initAttrs()
//初始化视图
initView(context)
//点击事件
setupEvent()
}
private fun setupEvent() {
mBtnMinus.setOnClickListener {
mCurrentNumber -= mStep
if (mCurrentNumber <= mMin) {
mCurrentNumber = mMin
it.isEnabled = false
}
mBtnPlus.isEnabled = true
updateEtv() //更新数据
}
mBtnPlus.setOnClickListener {
mCurrentNumber += mStep
if (mCurrentNumber >= mMax) {
mCurrentNumber = mMax
it.isEnabled = false
}
mBtnMinus.isEnabled = true
updateEtv() //更新数据
}
}
private fun initView(context: Context) {
val view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, true)
mBtnMinus = view.findViewById(R.id.btn_minus)
mBtnPlus = view.findViewById(R.id.btn_plus)
mEtv = view.findViewById(R.id.edit_text)
updateEtv() //用户设置默认值时,更新默认值
Log.d("qin", "$mDisable")
mBtnPlus.isEnabled = !mDisable
mBtnPlus.isEnabled = !mDisable
}
private fun initAttrs() {
//获取相关属性
val a = context.obtainStyledAttributes(R.styleable.InputNumberView)
mMin = a.getInt(R.styleable.InputNumberView_min, 0)
mMax = a.getInt(R.styleable.InputNumberView_max, 30)
mStep = a.getInt(R.styleable.InputNumberView_step, 1)
mDisable = a.getBoolean(R.styleable.InputNumberView_disable, true)
Log.d("InputNumberView", "get---$mDisable")
mBtnColor = a.getResourceId(R.styleable.InputNumberView_btnColor, -1)
mDefaultValue = a.getInt(R.styleable.InputNumberView_default_value, 0)
mCurrentNumber = mDefaultValue
a.recycle() //回收数据
}
fun getNumber(): Int {
return mCurrentNumber
}
fun setNumber(value: Int) {
mCurrentNumber = value
updateEtv()
}
private fun updateEtv() {
mEtv.setText("$mCurrentNumber")
onNumberChangeListener?.invoke(mCurrentNumber) //更新数值变化
}
//设置默认值的函数
fun setDefaultValue(value:Int){
mDefaultValue = value
mCurrentNumber = mDefaultValue
updateEtv()
}
//设置步长的函数
fun setStep(step:Int){
mStep = step
}
}
自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="InputNumberView">
<attr name="min" format="integer"/>
<attr name="max" format="integer"/>
<attr name="step" format="integer"/>
<attr name="disable" format="boolean"/>
<attr name="btnColor" format="color|reference"/>
<attr name="default_value" format="integer"/>
</declare-styleable>
</resources>
//使用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".animation.MainActivity"
>
<com.ttyh.customview.animation.InputNumberView
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:min="0"
app:max="40"
app:disable="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
z在具体使用时出现获取不到xml中定义的自定义属性的情况
之前调用错方法了,已解决