我有对话片段。我打算在活动和对话中使用这个片段。我重写了 onCreateDialog 和 onCreateView 方法。这里是编码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.interval_time_popup, null);
setup(view, false);
return view;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.interval_time_popup, null);
builder.setTitle("Interval Time");
builder.setView(view);
setup(view, true);
builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
listener.setOnIntervalTime(hourNp.getValue(), minNp.getValue());
dismiss();
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
我像这样在活动类中使用这个片段。
SelectTimeIntervalDialogFragment fragment = new SelectTimeIntervalDialogFragment();
fragment.setHrMin(hr, min);
Bundle args = new Bundle();
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.shift_period_interval_layout, fragment);
t.commit();
我从另一个类似的活动中调用它。
if((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_NORMAL) ==
Configuration.SCREENLAYOUT_SIZE_NORMAL){
Intent intent = new Intent(ShiftPeriodActivity.this, SelectIntervalActivity.class);
intent.putExtra("intervalHr", speriod.intervalHr);
intent.putExtra("intervalMin", speriod.intervalMin);
startActivityForResult(intent, 1);
} else {
FragmentManager fm = getSupportFragmentManager();
intervalDialog = new SelectTimeIntervalDialogFragment();
intervalDialog.setHrMin(speriod.intervalHr, speriod.intervalMin);
intervalDialog.show(fm, "interval_fragment");
}
我有两个条件。当屏幕大小正常时,它会调用包含片段对话框的活动。否则,它会调用弹出对话框。当它调用弹出对话框时出现异常。它说requestFeature() must be called before adding content
。我可以这样使用吗?我想知道如何克服这一点。
谢谢。