一起使用datepicker和timepicker

时间:2013-09-02 18:21:24

标签: android android-datepicker android-timepicker

我正在尝试创建一个在同一个Activity中同时使用Datepickers和Timepickers对话框的应用。 我使用这些链接来帮助我弄清楚如何实现datepicker和timepicker: http://www.mkyong.com/android/android-time-picker-example/ http://www.mkyong.com/android/android-date-picker-example/

这两个链接在设计上非常相似。 但是,当我尝试一起使用它们时,只有一个对话框弹出而另一个不弹出。 Timepicker工作正常,它是第二个对话框。这是我的代码:

package com.example.meetingplanner;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MakeMeetingActivity extends Activity {

    //_____________________________

    public TextView CurrentDate;
    public TextView LabelDateTest;
    public Button Datebutton;

    public int year;
    public int month;
    public int day;

    static final int DATE_DIALOG_ID = 999;
    //_____________________________ 

    public TextView CurrentTime;
    public TextView LabelTimeTest;
    public Button TimeButton;

    public int hour;
    public int minute;

    static final int TIME_DIALOG_ID = 999;
    //_____________________________ 

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_makemeeting);

        setCurrentDateOnView();
        addListenerOnButtonDATE();

        setCurrentTimeOnView();
        addListenerOnButtonTIME();
    }

    public void setCurrentDateOnView() {

        CurrentDate = (TextView) findViewById(R.id.CurrentDate);
        LabelDateTest = (TextView) findViewById(R.id.DateTest);

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

        // set current date into textview
        CurrentDate.setText(new StringBuilder()
        // Month is 0 based, just add 1
        .append(month + 1).append("-").append(day).append("-")
        .append(year).append(" "));

    }

    public void addListenerOnButtonDATE() {

        Datebutton = (Button) findViewById(R.id.DateButton);

        Datebutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(DATE_DIALOG_ID);

            }

        });

    }

    protected Dialog onCreateDialogDATE(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerListener, 
                    year, month,day);
        }
        return null;
    }

    public DatePickerDialog.OnDateSetListener datePickerListener 
    = new DatePickerDialog.OnDateSetListener() {

        // when data is entered, everything will be appended
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            CurrentDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));
            LabelDateTest.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

        }
    };





    // display current time
    public void setCurrentTimeOnView() {

        CurrentTime = (TextView) findViewById(R.id.CurrentTime);
        LabelTimeTest = (TextView) findViewById(R.id.CurrentTime);

        final Calendar c = Calendar.getInstance();
        hour = c.get(Calendar.HOUR_OF_DAY);
        minute = c.get(Calendar.MINUTE);

        // set current time into textview
        CurrentTime.setText(
                new StringBuilder().append(pad(hour))
                .append(":").append(pad(minute)));

    }

    public void addListenerOnButtonTIME() {

        TimeButton = (Button) findViewById(R.id.TimeButton);

        TimeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog(TIME_DIALOG_ID);

            }

        });

    }

    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            // set time picker as current time
            return new TimePickerDialog(this, timePickerListener, hour, minute,false);

        }
        return null;
    }

    public TimePickerDialog.OnTimeSetListener timePickerListener = 
            new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int selectedHour,
                int selectedMinute) {
            hour = selectedHour;
            minute = selectedMinute;

            // set current time into textview
            CurrentTime.setText(new StringBuilder().append(pad(hour))
                    .append(":").append(pad(minute)));
            LabelTimeTest.setText(new StringBuilder().append(pad(hour))
                    .append(":").append(pad(minute)));

        }
    };

    public static String pad(int c) {
        if (c >= 10)
           return String.valueOf(c);
        else
           return "0" + String.valueOf(c);
    }
}

这就是我的代码,时间戳工作,但是datepicker什么都不做。一个类似但问题很少的问题是:Problem with datepicker and timepicker

编辑:我看到有些人认为DATE_DIALOG_IDTIME_DIALOG_ID应该有不同的值,但是当我这样做时,Datepicker按钮仍然不起作用

0 个答案:

没有答案