public class MainActivity extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Use the current time as the default values for the picker
final 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,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}}
答案 0 :(得分:3)
在DialogFragment
中,您必须使用onCreateView
而不是setContentView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View view = inflater.inflate(R.layout.activity_main, container,
false);
return view;
}
和TimePickerFragment
使用TimePickerFragment.newInstance()
致电并newInstance
DialogFragment
构造函数
DialogFragment dialogFrag = null;
dialogFrag = TimePickerFragment.newInstance();
dialogFrag.setTargetFragment(this, request_id);
dialogFrag.show(getChildFragmentManager().beginTransaction(),
"timepicker_dialog");