Web を検索したところ、このような TimePickerDialog を処理および管理する便利なコードが見つかりました
私はそれを試してみましたが、動作しますが、フラグメントに拡張するクラスに統合すると、ボタンのクリック時に TimePickerDialog が表示されません
これをどのように実装できますか?
public class First_Activity extends Fragment implements ActionBar.TabListener {
static final int TIME_DIALOG_ID = 1111;
public Button btnClick;
TextView STtime;
int hour,minute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
STtime = (TextView) getActivity().findViewById(R.id.textView2);
/********* display current time on screen Start ********/
final Calendar c = Calendar.getInstance();
// Current Hour
hour = c.get(Calendar.HOUR_OF_DAY);
// Current Minute
minute = c.get(Calendar.MINUTE);
// set current time into output textview
updateTime(hour, minute);
/********* display current time on screen End ********/
btnClick = (Button) getActivity().findViewById(R.id.button4);
btnClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getActivity().showDialog(TIME_DIALOG_ID);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
return new TimePickerDialog(getActivity(), timePickerListener, hour, minute,
false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
// TODO Auto-generated method stub
hour = hourOfDay;
minute = minutes;
updateTime(hour,minute);
}
};
private static String utilTime(int value) {
if (value < 10)
return "0" + String.valueOf(value);
else
return String.valueOf(value);
}
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
String minutes = "";
if (mins < 10)
minutes = "0" + mins;
else
minutes = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(hours).append(':')
.append(minutes).append(" ").append(timeSet).toString();
STtime.setText(aTime);
}
}