概述
改写
我结合了我们讨论过的开发人员指南中的两个例子.现在,您应该能够理解如何将用户从TimePickerFragment中选择的时间发送回更有用的Activity. (我从第一个例子做了一些改变.)
TimePickerFragment:
public class TimePickerFragment extends DialogFragment {
// Notice I removed "implements OnTimeSetListener" and changed the variables
private Activity mActivity;
private OnTimeSetListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mActivity = activity;
// This error will remind you to implement an OnTimeSetListener
// in your Activity if you forget
try {
mListener = (OnTimeSetListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnTimeSetListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
// I made a couple changes here!
return new TimePickerDialog(mActivity, mListener, hour, minute,
DateFormat.is24HourFormat(mActivity));
}
}
主要活动:
// add "implements OnTimeSetListener" to "public class MainActivity ..."
private int pickerHour = 0;
private int pickerMin = 0;
// onCreate() and your other methods...
public void showTimePickerDialog(View v) {
TimePickerFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
pickerHour = hourOfDay;
pickerMin = minute;
}
最后
以上就是老实乐曲为你收集整理的android中timepicker 常用属性,android – 如何读取TimePicker选择的值?的全部内容,希望文章能够帮你解决android中timepicker 常用属性,android – 如何读取TimePicker选择的值?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复