什么时候
我期待onDismiss
(或onCancel
)会被召唤.但是,它们都没有被调用.我可以知道有什么我想念的吗?从AlertDialog setOnDismissListener不起作用,我以为onCancel
当我按下后退按钮时会调用它.但它对我不起作用.我可以知道有什么我错过了吗?
public class RateAppDialogFragment extends SherlockDialogFragment {
public static RateAppDialogFragment newInstance() {
RateAppDialogFragment rateAppDialogFragment = new RateAppDialogFragment();
return rateAppDialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.rate_app_dialog_fragment, null);
Utils.setCustomTypeFace(view, Utils.ROBOTO_LIGHT_TYPE_FACE);
final AlertDialog dialog = new AlertDialog.Builder(this.getSherlockActivity())
.setTitle("Love JStock?")
.setView(view)
// Add action buttons
.setPositiveButton("Rate 5 stars \u2605", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Rate");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("No");
}
})
.setNeutralButton("Later", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Later");
}
})
.create();
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Utils.showShortToast("Back button pressed?");
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Utils.showShortToast("Back button pressed?");
}
});
return dialog;
}
}
Run Code Online (Sandbox Code Playgroud)
FragmentManager fm = fragmentActivity.getSupportFragmentManager();
if (fm.findFragmentByTag(RATE_APP_DIALOG_FRAGMENT) != null) {
return;
}
RateAppDialogFragment rateAppDialogFragment = RateAppDialogFragment.newInstance();
rateAppDialogFragment.show(fm, RATE_APP_DIALOG_FRAGMENT);
Run Code Online (Sandbox Code Playgroud)
Che*_*eng 136
当您使用DialogFragment
显示时会发生此问题Dialog
据http://developer.android.com/reference/android/app/DialogFragment.html,解决的办法是覆盖onCancel
在DialogFragment
注意:DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调.你不能自己设置它们.要了解这些事件,请覆盖onCancel(DialogInterface)和onDismiss(DialogInterface).
// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}
Run Code Online (Sandbox Code Playgroud)
如果您正在使用AlertDialog
,请参阅
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// dialog dismiss without button press
}
});
Run Code Online (Sandbox Code Playgroud)
和dialog.setCanceledOnTouchOutside(true)
(谢谢@LeosLiterak)
您还没有设置backPressed
按键事件
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
51902 次 |
最近记录: |