我如何定制时间选择器对话分钟进度15分钟

时间:2014-03-26 20:14:01

标签: android

我正在为汽车预订开发Android应用程序,我希望每15分钟每隔一刻钟制作一个自定义TimePicker对话框。

我的代码:

package com.boosterbc.can;
import java.util.Calendar;
import java.util.Date;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class MainActivity extends Activity {
    Button b1; 
    final Calendar time= Calendar.getInstance();
    private int pHour = time.get(Calendar.HOUR);
    private int pMinute= time.get(Calendar.MINUTE);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=(Button)findViewById(R.id.bttime);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerDialog().show();      
            }
        });
    } 
    public Dialog TimePickerDialog()
    {
        TimePickerDialog.OnTimeSetListener t= new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
                pHour=hourOfDay;
                pMinute=minute; 
            }
        };
        imePickerDialog dialog = new TimePickerDialog(this,t,pHour, pMinute,true);
        return dialog;
    }
}

1 个答案:

答案 0 :(得分:0)

而不是TimePickerDialog,而是在其布局上使用DialogFragment,其中包含两个NumberPicker:第一个为小时,值为0到23,第二个为分钟,值为0到3和自定义显示值:

numberPickerMinutes.setMinValue(0);
numberPickerMinutes.setMaxValue(3);
numberPickerMinutes.setDisplayedValues(new String[]{"00", "15", "30", "45"});

用户从分钟选择器中选择一个值后,将其转换为分钟:

pMinute = numberPickerMinutes.getValue() * 15;