【问题标题】:Android NumberPicker: Set min, max, default from XMLAndroid NumberPicker:从 XML 设置最小值、最大值、默认值
【发布时间】:2012-09-01 07:53:24
【问题描述】:

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

我是在 Activity 代码中做的:

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

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

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

【问题讨论】:

  • 您可能需要自定义 NumberPicker,然后在任何地方使用 MyNumberPicker...并在 MyNumberPicker 的构造函数中获取所有属性集和设置值...
  • @MKJParekh 你知道如何为时间选择器设置 setMaxValue setMinValue 吗? stackoverflow.com/questions/20188983/…

标签: android xml layout numberpicker


【解决方案1】:

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

  1. 我创建了自己的 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));
        }
    }
    
  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" />
    

感谢 MKJParekh 的有用评论

【讨论】:

  • 效果很好,谢谢分享。在 Eclipse 中使用图形布局编辑器时请注意:如果您在此处更改任何内容,则 min 和 max 等自定义属性将被删除,您必须再次手动添加它们。
  • 请注意,最好在 res/values/attrs.xml 中定义自定义 XML 属性,如 docs 中所述。这也使 IDE / 布局编辑器能够识别自定义属性。文档还说不应直接使用AttributeSet(如此处),而应检索TypedArray
  • 感谢分享。非常惊讶 Google“忘记”将这些基本属性放入基类中。
  • 非常感谢,您也可以覆盖 setValue 以便在 XML 文件中进行设置。
  • @MazeChaZer - 我认为my answer 应该只是你的评论。如果您想将其合并到您的答案中,那么我将删除我的。
【解决方案2】:

你可以试试这个:

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

【讨论】:

    【解决方案3】:

    这是遵循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 = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.NumberPickerWithXml, defStyleAttr, defStyleRes);
    
            try {
                this.setMinValue(attributes.getInt(R.styleable.NumberPickerWithXml_minValue, 0));
                this.setMaxValue(attributes.getInt(R.styleable.NumberPickerWithXml_maxValue, 0));
                this.setValue(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>
    

    【讨论】:

    • 验证 Java 代码的一种超级简单的方法是将其放入 Android Studio 中的 Java 文件中。它会很快指出所有错误。
    • @AbandonedCart - 感谢您修复它! Java 版本是事后才想到的。我并没有想到我可以将一个 Java 文件添加到我的纯 Kotlin 项目中!今天反思一下,我认为我的回答应该只是 MazeChaZer 回答下的评论。
    • 就个人而言,我更喜欢这个。它更完整,更容易理解,这两者都可能作为评论丢失了。
    • 我会一直坚持到它过时 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2017-07-14
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多