时间选择器setText为12小时格式

时间:2016-09-30 09:18:14

标签: android timepicker

我在一个对话框上有一个时间选择器,它在选择的时间设置EditText,但每当我选择时间格式的时间是军事时间..我怎样才能使它成为12小时格式。 Thaaanks:)

@Override
protected Dialog onCreateDialog(int id){
    if(id == DIALOG_ID){
        return new DatePickerDialog(this, dplistener, year_x,month_x,day_x);
    }

    else if (id == DIALOG_ID2){
        return new TimePickerDialog(this, tplistener, hour_x, minute_x, DateFormat.is24HourFormat(DetailActivity.this));
    }

    return null;
}


 protected TimePickerDialog.OnTimeSetListener tplistener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                hour_x = hourOfDay;
                minute_x = minute;
                etResTime.setText(hour_x +" : " + minute_x );
            }
        };

2 个答案:

答案 0 :(得分:0)

timePicker.setIs24HourView(假);

答案 1 :(得分:0)

protected TimePickerDialog.OnTimeSetListener tplistener =
        new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                int hour = hourOfDay % 12;
                if (hour == 0) hour = 12;
                String _AM_PM = (hourOfDay > 12) ? "PM" : "AM";
                etResTime.setText(String.format(Locale.getDefault(), "%02d:%02d %s", hour, minute, _AM_PM));
            }
        };