时间选择器最短时间

时间:2017-03-27 12:42:32

标签: android timepicker

我正在使用TimePicker来选择时间,我想在选择器中设置最短时间以限制用户选择过去的时间。有什么办法可以实现吗?

2 个答案:

答案 0 :(得分:0)

创建一个xml文件并将其命名为activity_time_picker.xml

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

<TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layoutHourMinute"
        android:layout_centerInParent="true"

        />

</RelativeLayout>

现在创建像这样的活动类

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.NumberPicker;
import android.widget.TimePicker;

public class TimePickerActivity extends Activity {


    TimePicker timePicker;
    private int TIME_PICKER_INTERVAL = 15;
     NumberPicker minutePicker;
     List<String> displayedValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_time_picker);
        super.onCreate(savedInstanceState);
        timePicker = (TimePicker)findViewById(R.id.timePicker1);

        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(0);
        timePicker.setCurrentMinute(0);

        setTimePickerInterval(timePicker);
    }
@SuppressLint("NewApi")
    private void setTimePickerInterval(TimePicker timePicker) {
         try {
                Class<?> classForid = Class.forName("com.android.internal.R$id");
               // Field timePickerField = classForid.getField("timePicker");  

                Field field = classForid.getField("minute");
                minutePicker = (NumberPicker) timePicker
                        .findViewById(field.getInt(null));

                minutePicker.setMinValue(0);
                minutePicker.setMaxValue(3);
                displayedValues = new ArrayList<String>();
                for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                    displayedValues.add(String.format("%02d", i));
                }
              //  for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
              //      displayedValues.add(String.format("%02d", i));
              //  }
                minutePicker.setDisplayedValues(displayedValues
                        .toArray(new String[0]));
               minutePicker.setWrapSelectorWheel(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

答案 1 :(得分:0)

嘿使用它创建一个DatePicker片段如下:

    public class DatePickerFragment1 extends DialogFragment {
    DatePickerDialog.OnDateSetListener ondateSet;
    private int year, month, day;

    public DatePickerFragment1() {}

    public void setCallBack(DatePickerDialog.OnDateSetListener ondate) {
        ondateSet = ondate;
    }

    @SuppressLint("NewApi")
    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        year = args.getInt("year");
        month = args.getInt("month");
        day = args.getInt("day");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog dpd = new DatePickerDialog(getActivity(),AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,ondateSet,year, month, day);
        DatePicker dp = dpd.getDatePicker();

//here it gets the current time and subtracts the time of min date in my case 
//I have selected 3months from current time
        dp.setMinDate(System.currentTimeMillis() - 5184000000L);
        c.add(Calendar.DAY_OF_MONTH, 60);
//current time as max date so it will be displayed as you enter date dialog
        dp.setMaxDate(System.currentTimeMillis());

        return dpd;

    }
}

然后在您的片段或活动中将其称为:

    DatePickerFragment1 date = new DatePickerFragment1();
    /**
     * Set Up Current Date Into dialog
     */
    Calendar calender = Calendar.getInstance();
    Bundle args = new Bundle();
    args.putInt("year", calender.get(Calendar.YEAR));
    args.putInt("month", calender.get(Calendar.MONTH));
    args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
    date.setArguments(args);
    /**
     * Set Call back to capture selected date
     */
    date.setCallBack(ondate1);

    date.show(getActivity().getFragmentManager(), "Date Picker");
}

public String checkDigit(int number) {
    return number <= 9 ? "0" + number : String.valueOf(number);
}

DatePickerDialog.OnDateSetListener ondate1 = new DatePickerDialog.OnDateSetListener() {


        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {

            etDatePurchase.setText(String.valueOf(checkDigit(dayOfMonth)) + "/" + String.valueOf(checkDigit(monthOfYear + 1))
                    + "/" + String.valueOf(year));

        }

    };