Android NumberPicker:从XML设置最小值、最大值、默认值

Android NumberPicker:从XML设置最小值、最大值、默认值,android,xml,layout,numberpicker,Android,Xml,Layout,Numberpicker,是否有一种方法可以从XML布局中设置a的最小值、最大值和默认值 我是在活动代码中进行的: np = (NumberPicker) findViewById(R.id.np); np.setMaxValue(120); np.setMinValue(0); np.setValue(30); XML显然更合适,因为它定义的是属性,而不是行为 有没有办法使用XML布局设置这些内容?我也遇到了同样的问题,我就是这样解决的(根据MKJParekh的评论): 我创建了自己的NumberPicker类 pa

是否有一种方法可以从XML布局中设置a的最小值、最大值和默认值

我是在活动代码中进行的:

np = (NumberPicker) findViewById(R.id.np);
np.setMaxValue(120);
np.setMinValue(0);
np.setValue(30);
XML显然更合适,因为它定义的是属性,而不是行为


有没有办法使用XML布局设置这些内容?

我也遇到了同样的问题,我就是这样解决的(根据MKJParekh的评论):

  • 我创建了自己的NumberPicker类

    package com.exaple.project;
    
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.os.Build;
    import android.util.AttributeSet;
    import android.widget.NumberPicker;
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)//For backward-compability
    public class MyNumberPicker extends NumberPicker {
    
        public MyNumberPicker(Context context) {
            super(context);
        }
    
        public MyNumberPicker(Context context, AttributeSet attrs) {
            super(context, attrs);
            processAttributeSet(attrs);
        }
    
        public MyNumberPicker(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            processAttributeSet(attrs);
        }
        private void processAttributeSet(AttributeSet attrs) {
            //This method reads the parameters given in the xml file and sets the properties according to it
            this.setMinValue(attrs.getAttributeIntValue(null, "min", 0));
            this.setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
        }
    }
    
  • 现在,您可以在xml布局文件中使用此NumberPicker

    <com.exaple.project.myNumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        max="100"
        min="1" />
    
    
    

  • 感谢MKJParekh的有用评论

    以下是更新版 (因此支持主题化和Android Studio designer预览)

    values/attrs.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="NumberPickerWithXml">
            <attr name="maxValue" format="integer" />
            <attr name="minValue" format="integer" />
            <attr name="defaultValue" format="integer" />
        </declare-styleable>
    
    </resources>
    
    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()
            }
        }
    
    }
    
    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();
            }
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <com.example.library.ui.NumberPickerWithXml
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            custom:defaultValue="30"
            custom:maxValue="120"
            custom:minValue="0" />
    
    </LinearLayout>
    
    …或NumberPickerWithXml.java(未经测试):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="NumberPickerWithXml">
            <attr name="maxValue" format="integer" />
            <attr name="minValue" format="integer" />
            <attr name="defaultValue" format="integer" />
        </declare-styleable>
    
    </resources>
    
    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()
            }
        }
    
    }
    
    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();
            }
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <com.example.library.ui.NumberPickerWithXml
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            custom:defaultValue="30"
            custom:maxValue="120"
            custom:minValue="0" />
    
    </LinearLayout>
    
    布局中的用法:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <declare-styleable name="NumberPickerWithXml">
            <attr name="maxValue" format="integer" />
            <attr name="minValue" format="integer" />
            <attr name="defaultValue" format="integer" />
        </declare-styleable>
    
    </resources>
    
    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()
            }
        }
    
    }
    
    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();
            }
        }
    
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <com.example.library.ui.NumberPickerWithXml
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            custom:defaultValue="30"
            custom:maxValue="120"
            custom:minValue="0" />
    
    </LinearLayout>
    
    
    
    您可以尝试以下方法:

    <NumberPicker
      android:id="@+id/number_picker"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:minValue="@{0}"
      app:maxValue="@{120}"
      app:value="@{30}"/>
    

    您可能需要自定义NumberPicker,然后在任何地方使用它MyNumberPicker…在MyNumberPicker的构造函数中获取所有属性集和设置值…@MKJParekh您知道如何为时间选择器设置setMaxValue setMinValue吗?很好用,谢谢分享。在Eclipse中使用图形布局编辑器时请注意:如果您在此处更改任何内容,则会删除自定义属性(如min和max),您必须再次手动添加它们。请注意,最好在
    res/values/attrs.XML
    中定义自定义XML属性,如中所述。这还使IDE/布局编辑器能够识别自定义属性。文档还说,不应直接使用
    属性集
    (如此处所示),而应检索
    类型Darray
    。感谢分享。非常惊讶Google“忘记”将这些基本属性放在基类中。非常感谢,您还可以重写setValue,以便在XML文件中进行设置。