在创建自定义Progress DialogFragment时,我有点困惑。一切正常,但由于我不希望用户“撤回”直到DialogFragment被解除,我正在尝试捕获KeyEvent并“禁用”。
虽然这很有效:
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setMessage(getString(R.string.loading_text));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
// Disable the back button
OnKeyListener keyListener = new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return false;
}
};
dialog.setOnKeyListener(keyListener);
return dialog;
}
使用onCreateDialog不会让我正确地膨胀片段,因此,自定义片段的外观和感觉。另一方面,onCreateDialog完美地捕获了按键事件。切换到onCreateView时:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the XML view for the help dialog fragment
View view = inflater.inflate(R.layout.progress_dialog_fragment, container);
TextView text = (TextView)view.findViewById(R.id.loadingMessage);
text.setText(Html.fromHtml(getString(R.string.loading_text)));
// Disable the back button
android.view.View.OnKeyListener keyListener = new android.view.View.OnKeyListener() {
@Override
public boolean onKey(View view, int keyCode, KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_BACK){
return true;
}
return false;
}
};
view.setOnKeyListener(keyListener);
// request a window without the title
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//Transparent Dialog background
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0));
return view;
}
事件未被捕获。这引出了另一个问题......因为我已经看到在许多SO问题中使用onCreateDialog和onCreateView ......这两者之间的区别是什么?
谢谢!
答案 0 :(得分:6)
DialogFragment newFragment = YourFragment.newInstance();
newFragment.setCancelable(false);
newFragment.show(fragmentTransaction, "dialog");
在.schow()片段
之前添加setCancelable(false)答案 1 :(得分:3)
onCreateDialog
vs onCreateView
:
实现应覆盖此类并实现 onCreateView(LayoutInflater,ViewGroup,Bundle)提供内容 对话框。或者,他们可以覆盖onCreateDialog(Bundle) 使用它创建一个完全自定义的对话框,例如AlertDialog 自己的内容。
使用onCreateDialog
时很重要:
覆盖以构建您自己的自定义Dialog容器。这通常是 用于显示AlertDialog而不是通用Dialog;什么时候做 所以,onCreateView(LayoutInflater,ViewGroup,Bundle)确实不需要 由于AlertDialog会处理自己的内容,因此可以实施。
示例Dialog
(在本例中为AlertDialog
):
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setCanceble(false)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();
}
}
属性setCanceble(boolean)
说明您是否可以通过反压退出Dialog
。无需在任何地方捕获KEYCODE_BACK。