在创建自定义进度对话框片段时,我在这里有点困惑。一切正常,但由于我不希望用户“返回”直到 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 并不能让我正确地为 Fragment 充气,因此,自定义我的 Fragment 的外观和感觉。另一方面,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 ......两者之间有什么区别?
谢谢!