Android NumberPicker: Set min, max, default from XML

前端 未结 2 1651
逝去的感伤
逝去的感伤 2020-12-08 06:33

Is there a way to set the minimum, maximum and default values of a NumberPicker from the XML Layout?

I\'m doing it from within the Activity code:

np          


        
2条回答
  •  执念已碎
    2020-12-08 06:39

    Here is an updated version that follows the Android Docs
    (and thus supports theming & Android Studio designer preview)

    values/attrs.xml:

    
    
    
        
            
            
            
        
    
    
    

    NumberPickerWithXml.kt:

    package com.example.library.ui
    
    import android.content.Context
    import android.util.AttributeSet
    import android.widget.NumberPicker
    import com.example.library.ui.R
    
    class NumberPickerWithXml : NumberPicker {
    
        constructor(context: Context) : super(context)
    
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
            processXmlAttributes(attrs)
        }
    
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
            processXmlAttributes(attrs, defStyleAttr)
        }
    
        constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
            processXmlAttributes(attrs, defStyleAttr, defStyleRes)
        }
    
        private fun processXmlAttributes(attrs: AttributeSet, defStyleAttr: Int = 0, defStyleRes: Int = 0) {
            val attributes = context.theme.obtainStyledAttributes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes)
    
            try {
                this.minValue = attributes.getInt(R.styleable.NumberPickerWithXml_minValue, 0)
                this.maxValue = attributes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0)
                this.value = attributes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0)
            } finally {
                attributes.recycle()
            }
        }
    
    }
    

    ...or NumberPickerWithXml.java (untested):

    package com.example.library.ui
    
    import android.content.Context
    import android.util.AttributeSet
    import android.widget.NumberPicker
    import com.example.library.ui.R
    
    public class NumberPickerWithXml extends NumberPicker {
    
        public NumberPickerWithXml(Context context) {
            super(context);
        }
    
        public NumberPickerWithXml(Context context, AttributeSet: attrs) {
            super(context, attrs);
            processXmlAttributes(attrs, 0, 0);
        }
    
        public NumberPickerWithXml(Context context, AttributeSet: attrs, int: defStyleAttr) {
            super(context, attrs, defStyleAttr);
            processXmlAttributes(attrs, defStyleAttr, 0);
        }
    
        public NumberPickerWithXml(Context context, AttributeSet: attrs, int: defStyleAttr, int: defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            processXmlAttributes(attrs, defStyleAttr, defStyleRes);
        }
    
        private void processXmlAttributes(AttributeSet: attrs, int: defStyleAttr, int: defStyleRes) {
            TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes)
    
            try {
                this.minValue = attributes.getInt(R.styleable.NumberPickerWithXml_minValue, 0);
                this.maxValue = attributes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0);
                this.value = attributes.getInt(R.styleable.NumberPickerWithXml_defaultValue, 0);
            } finally {
                attributes.recycle();
            }
        }
    
    }
    

    Usage in your layout:

    
    
    
        
    
    
    

提交回复
热议问题