2014-01-16 74 views
0

我在做一個android應用程序,我必須選擇特定的時間。在Android TimePicker對話框片段中顯示12hh格式

這是我到目前爲止已經完成:

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    // use the current time as the default values for the picker 
    Calendar c = Calendar.getInstance(); 
    int hour = c.get(Calendar.HOUR_OF_DAY); 
    int minute = c.get(Calendar.MINUTE); 


    // create a new instance of TimePickerDialog and return it 
    return new TimePickerDialog(getActivity(), this, hour, minute, false 
      ); // DateFormat.is24HourFormat(getActivity()) 
} 

@Override 
public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
{ 
    view.setIs24HourView(false); 
    String time; 

    if (minute < 10 ) { 
     _minute = ":0" + minute; 
    } else { 
     _minute = ":" + minute; 
    } 


    Log.w("TimePicker", "Time = " + time); 


    EditText activityButton = (EditText)getActivity().findViewById(R.id.etTime); 
    activityButton.setText(time); 


} 

我想實現的是:

6:30 PM 

但我得到的是:

18:30 

我想12H格式不是24小時。我怎樣才能做到這一點?非常感謝幫助。

+1

http://www.compiletimeerror.com/2013/ 07 /時間選擇器功能於Android的example.html的#.Utd3-t_FsUQ。檢查這是否有幫助 – Raghunandan

+0

謝謝你解決我的問題! – Dunkey

+0

@Raghunandan該網址解決方案的問題是中午12:00 PM變成了12:00。 – Gordons

回答

0

你只要把一個條件,以確定當天的小時值大於12,所以你可以通過12酌減小時並將PM附加到它。該條件還應該確定一天中的小時是否等於下午12點,因此它將在不減少12的情況下追加PM。

if (hourOfDay > 12) { 
    hourOfDay = hourOfDay - 12; 
    String AM_PM = "PM"; 
} else if (hourOfDay < 12) { 
    String AM_PM = "AM"; 
}else{ 
    String AM_PM = "PM"; // this means it is 12 in the afternoon 
}