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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12317960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:24:17  来源:igfitidea点击:

Android NumberPicker: Set min, max, default from XML

androidxmllayoutnumberpicker

提问by Adam Matan

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

有没有办法从 XML 布局设置NumberPicker的最小值、最大值和默认值?

I'm doing it from within the Activity code:

我在活动代码中这样做:

np = (NumberPicker) findViewById(R.id.np);
np.setMaxValue(120);
np.setMinValue(0);
np.setValue(30);

XML is obviously more appropriate , because it defines property, not behaviour.

XML 显然更合适,因为它定义了属性,而不是行为。

Is there a way to set these using the XML layout?

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

回答by MazeChaZer

I had the same problem, this is how I solved it (according to the comment of MKJParekh):

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

  1. I created my own NumberPicker-Class

    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));
        }
    }
    
  2. Now you can use this NumberPicker in your xml layout file

    <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" />
    
  1. 我创建了自己的 NumberPicker-Class

    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));
        }
    }
    
  2. 现在你可以在你的 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" />
    

Thanks to MKJParekh for his useful comment

感谢 MKJParekh 的有用评论

回答by AjahnCharles

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

这是遵循Android Docs的更新版本
(因此支持主题和 Android Studio 设计器预览)

values/attrs.xml:

值/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>

NumberPickerWithXml.kt:

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):

...或 NumberPickerWithXml.java(未经测试):

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:

在您的布局中的用法:

<?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>