【问题标题】:Android create custom NumberPicker widgetAndroid 创建自定义 NumberPicker 小部件
【发布时间】:2015-02-17 11:02:14
【问题描述】:

我在应用程序中使用了一些 simvot 数字选择器,我想为此创建自定义小部件并在所有项目中使用,现在我正在创建这些文件,但我无法为自定义小部件设置任何值。

我的属性:

<declare-styleable name="CNumberPicker">
    <attr name="maxValue"               format="string" />
    <attr name="minValue"               format="string" />
    <attr name="value"                  format="string" />
</declare-styleable>

我的自定义 NumberPicker 小部件:

public class CNumberPicker extends net.simonvt.numberpicker.NumberPicker implements OnClickListener {

    private int maxValue;
    private int minValue;
    private int value;

    public CNumberPicker(Context context) {
        super(context);
    }

    public CNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        processAttributeSet(attrs);
    }

    public CNumberPicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CNumberPicker, defStyle, 0);
        processAttributeSet(attrs);

        maxValue = Integer.parseInt(a.getString(R.styleable.CNumberPicker_maxValue));
        minValue = Integer.parseInt(a.getString(R.styleable.CNumberPicker_minValue));

        a.recycle();
        setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

    }


    private void processAttributeSet(AttributeSet attrs) {
        this.setMaxValue(attrs.getAttributeIntValue(null, "maxValue", 0));
    }

}

我定义的小部件到 xml 中:

<com.sample.widget.CNumberPicker
    android:id="@+id/np_choose_hour"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="10dp"
    android:layout_weight="0.20"
    android:gravity="center_horizontal"
    app:maxValue="10"
    app:minValue="1"
    app:value="5">
</com.sample.widget.CNumberPicker>

如何在布局中设置定义到xml中的值?

【问题讨论】:

    标签: android


    【解决方案1】:

    你必须在代码中扩充布局。

    public CNumberPicker(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.cnumberpicker, this);
    }
    

    假设布局文件是 cnumberpicker.xml

    编辑:这 3 个函数是构造函数。可以调用其中任何一个来构造对象。通过使用this 而不是super,我们可以强制所有构造函数最终调用3 参数构造函数。你需要一些凝聚力。您不希望每次进行更改时都复制粘贴代码。

    public CNumberPicker(Context context) {
        this(context, null);
    }
    
    public CNumberPicker(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public CNumberPicker(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CNumberPicker, defStyle, 0);
        processAttributeSet(attrs);
    
        // this method allows a default value if no attributes are given. You should use them.
        maxValue = Integer.parseInt(a.getString(R.styleable.CNumberPicker_maxValue,"10"));
        minValue = Integer.parseInt(a.getString(R.styleable.CNumberPicker_minValue,"0"));
        a.recycle();
    }
    

    【讨论】:

    • 我收到getLayoutInflater() 的错误以及如何为np_choose_hour 定义布局?我有一些数字选择器,例如年、月、日和 ...np_choose_hour 必须是程序更改
    • 已编辑。它必须是布局文件夹中 xml 文件的名称。
    • 谢谢。我现在收到此错误:Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer 我的小部件无法从属性中获取数据,maxValue 为空,我无法设置它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2023-03-20
    • 2019-11-20
    • 2021-08-16
    相关资源
    最近更新 更多