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

时间:2014-02-07 11:26:06

标签: java android

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

我如何获得numberpicker的值并像TimePickerDialog一样移动

1 个答案:

答案 0 :(得分:0)

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

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> 

希望这是你需要的