Android 时间选择器对话框将不显示

Android 时间选择器对话框将不显示,android,timepicker,Android,Timepicker,我是新来的,很抱歉,我已经搜索过了,但在以前的问题中找不到答案。 我有一个日期选择器和时间选择器,但当我运行它时,时间选择器对话框不会显示。有什么想法吗?我搜索了几个小时的答案,这是我的代码: public class BuildingFireActivity extends Activity { private Button buildingfirePickDate; private Button buildingfiredispatchPickTime; private Text

我是新来的,很抱歉,我已经搜索过了,但在以前的问题中找不到答案。 我有一个日期选择器和时间选择器,但当我运行它时,时间选择器对话框不会显示。有什么想法吗?我搜索了几个小时的答案,这是我的代码:

    public class BuildingFireActivity extends Activity {

private Button buildingfirePickDate;
private Button buildingfiredispatchPickTime;

private TextView buildingfireDateDisplay;
private TextView buildingfiredispatchTimeDisplay;

private int buildingfireYear;
private int buildingfireMonth;
private int buildingfireDay;
private int buildingfiredispatchHour;
private int buildingfiredispatchMinute;

static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buildingfirelayout);

    buildingfirePickDate = (Button) findViewById(R.id.pickDate);
    buildingfiredispatchPickTime = (Button) findViewById(R.id.dispatchTime);

    buildingfireDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    buildingfiredispatchTimeDisplay = (TextView) findViewById(R.id.dispatchDisplay);

    buildingfirePickDate.setOnClickListener(new View.OnClickListener() {


@SuppressWarnings("deprecation")
public void onClick(View v) {
    showDialog(DATE_DIALOG_ID);}});
    final Calendar c = Calendar.getInstance();
    buildingfireYear = c.get(Calendar.YEAR);
    buildingfireMonth = c.get(Calendar.MONTH);
    buildingfireDay = c.get(Calendar.DAY_OF_MONTH);
    updateDisplay();
    buildingfiredispatchPickTime.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
    showDialog(TIME_DIALOG_ID);}});
    final Calendar dispatch = Calendar.getInstance();
    buildingfiredispatchHour = dispatch.get(Calendar.HOUR_OF_DAY);
    buildingfiredispatchMinute = dispatch.get(Calendar.MINUTE);
    updateDisplay1();}
private static String pad(int dispatch) {
    if (dispatch >= 10)
    return String.valueOf(dispatch);
    else
    return "0" + String.valueOf(dispatch);}


private void updateDisplay() {
    buildingfireDateDisplay.setText(
    new StringBuilder()
    .append(buildingfireMonth + 1).append("-")
    .append(buildingfireDay).append("-")
    .append(buildingfireYear).append(" "));}
private void updateDisplay1() {
    buildingfiredispatchTimeDisplay.setText(
    new StringBuilder()
    .append(pad(buildingfiredispatchHour)).append(":")
    .append(pad(buildingfiredispatchMinute)));}

private DatePickerDialog.OnDateSetListener buildingfireDateSetListener =
    new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            buildingfireYear = year;
            buildingfireMonth = monthOfYear;
            buildingfireDay = dayOfMonth;
            updateDisplay();}};
private TimePickerDialog.OnTimeSetListener buildingfiredispatchTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            buildingfiredispatchHour = hourOfDay;
            buildingfiredispatchMinute = minute;
            updateDisplay1();}}; 



protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
    return new TimePickerDialog(this,
            buildingfiredispatchTimeSetListener, 
            buildingfiredispatchHour, 
            buildingfiredispatchMinute, false);}
            return null;}

@Override
protected Dialog onCreateDialog1(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
    return new DatePickerDialog(this,
            buildingfireDateSetListener,
            buildingfireYear, 
            buildingfireMonth, 
            buildingfireDay);}
            return null;}}

尝试使用此代码并删除onCreateDialog1。

您好,您可以尝试此代码吗?我希望查看日期选择器对话框会有所帮助

从对话框中选择日期并在文本视图中显示

public class DatePickerDemoActivity extends Activity {
/** Called when the activity is first created. */

   private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // capture our View elements
    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    mPickDate = (Button) findViewById(R.id.pickDate);

    // add a click listener to the button
    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date (this method is below)
    updateDisplay();
}

// updates the date in the TextView
private void updateDisplay() {
    mDateDisplay.setText(getString(R.string.strSelectedDate,
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" ")));
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    }
    return null;
}
}

谢谢,非常有帮助!对于清理或减少这方面的代码量,您还有其他建议吗?我也试图解决您的问题。在我的例子中,datepicker对话框抛出一个错误,timepicker对话框显示,但是您有不必要的警告。删除它们。尝试编写格式化代码。至少使用eclipse格式化程序格式化代码。这样其他人就可以很容易地理解您的代码。如果你想学习,试着编写你自己的代码。通过复制你只会得到你的结果,而不是学习部分。谢谢你,也很有帮助,我不知道格式功能。
public class DatePickerDemoActivity extends Activity {
/** Called when the activity is first created. */

   private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // capture our View elements
    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    mPickDate = (Button) findViewById(R.id.pickDate);

    // add a click listener to the button
    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date (this method is below)
    updateDisplay();
}

// updates the date in the TextView
private void updateDisplay() {
    mDateDisplay.setText(getString(R.string.strSelectedDate,
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" ")));
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    }
    return null;
}
}