其他分享
首页 > 其他分享> > 时间选择器Android

时间选择器Android

作者:互联网

如何在TimePicker中设置先前选择的值?

我的意思是,单击TextView,出现TimePicker对话框,然后选择时间并在TextView中进行设置,例如下午12:30在TextView上.

此后,如果要更改时间,请再次单击TextView,然后“ TimePicker”对话框应显示先前选择的值.因此,TimePicker应该显示12:30 PM.

我怎样才能做到这一点?

解决方法:

如果使用onCreateDialog创建时间选择器对话框并调用它,它将自动存储以前的值.

@Override
protected Dialog onCreateDialog(int id) {

switch (id) {

    case 0:

TimePickerDialog timeDlg = new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                            int minute) {
                        // TODO Auto-generated method stub

                        if (hourOfDay > 12) {
                            hourOfDay = hourOfDay - 12;
                            time = " PM";
                        } else if (hourOfDay < 12 && hourOfDay != 0) {
                            time = " AM";
                        } else if (hourOfDay == 12) {
                            time = " PM";
                        } else if (hourOfDay == 0) {
                            hourOfDay = 12;
                            time = " AM";
                        }

                        Toast.makeText(
                                getApplicationContext(),
                                new StringBuilder().append(pad(hourOfDay))
                                        .append(":").append(pad(minute))
                                        .append(time), Toast.LENGTH_SHORT)
                                .show();
                    }
                }, 12, 00, false);

        timeDlg.setMessage("Set Time:");

        timeDlg.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "Dismiss",
                        Toast.LENGTH_SHORT).show();
            }
        });
        return timeDlg;
}
    return null;
}

使用showDialog(id);显示对话框.

标签:timepicker,android,textview
来源: https://codeday.me/bug/20191201/2080442.html