我想制作没有对话框的数字选择器

最后发布: 2014-02-07 11:26:06


问题
<NumberPicker
    android:id="@+id/firstSeekBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/image3" />

我如何获得数字选择器的值并像TimePickerDialog一样移动

java android
回答

这是我用于日期和时间的号码选择器:

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 NumberPickerMinMax extends NumberPicker
{
    public NumberPickerMinMax(Context context) 
    {
        super(context);
    }

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

    public NumberPickerMinMax(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));
    }
}

现在您需要为每个时间位数添加4 NumberPickerMinMax :HH:mm,这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <com.evapp.customViews.NumberPickerMinMax
            android:id="@+id/TimePickerView_hour2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            min="0"
            max="9"
            android:layout_toLeftOf="@+id/textView1" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text=" - "
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <com.evapp.customViews.NumberPickerMinMax
            android:id="@+id/TimePickerView_hour1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_margin="2dp"
            min="0"
            max="2"
            android:layout_toLeftOf="@+id/TimePickerView_hour2" />

        <com.evapp.customViews.NumberPickerMinMax
            android:id="@+id/TimePickerView_minutes1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            min="0"
            max="5"
            android:layout_toRightOf="@+id/textView1" />

        <com.evapp.customViews.NumberPickerMinMax
            android:id="@+id/TimePickerView_minutes2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            min="0"
            max="9"
            android:layout_toRightOf="@+id/TimePickerView_minutes1" />

    </RelativeLayout>

</RelativeLayout> 

希望这就是你所需要的