Android NumberPicker:从XML设置min,max,default

时间:2012-09-07 12:18:53

标签: android xml layout numberpicker

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

我是在活动代码中做到的:

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

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

有没有办法使用XML布局设置它们?

3 个答案:

答案 0 :(得分:55)

我有同样的问题,这是我解决它的方式(根据MKJParekh的评论):

  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" />
    
  3. 感谢MKJParekh的有用评论

答案 1 :(得分:6)

你可以试试这个:

<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}"/>

这是 Android 数据绑定的一部分。因此,您可能希望在应用级 build.gradle 中将其设置为 true。

android {
    ...

    dataBinding {
        enabled true
    }
}

答案 2 :(得分:5)

以下是Android Docs之后的更新版本 (从而支持主题和Android Studio设计器预览)

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

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()
        }
    }

}

...或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();
        }
    }

}

布局中的用法:

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