[英]Custom AlertDialog disappears on back button press
Hi guys I have a custom alert dialog I created. 大家好,我创建了一个自定义提醒对话框。 In the builder I set cancelable to false yet it still disappears when I press the back button, any ideas?
在构建器中,我将cancelable设置为false,但是当我按下后退按钮时它仍然消失了,有什么想法吗?
This is the code for the dialog: 这是对话框的代码:
public final class HemisphereDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View customTitle = inflater.inflate(R.layout.hemisphere_dialog_custom_title, null);
builder.setCustomTitle(customTitle);
String[] entries = new String[2];
entries[0] = getResources().getString(R.string.northern_hemisphere);
entries[1] = getResources().getString(R.string.southern_hemisphere);
builder.setItems(entries, new DialogInterface.OnClickListener() {
//The 'which' argument contains the index position of the selected item
public void onClick(DialogInterface dialog, int which) {
if( which == 0 ) {
GlobalVariables.getShared().setIsInNorthernHemisphere(true);
} else if( which == 1 ) {
GlobalVariables.getShared().setIsInNorthernHemisphere(false);
}
ToolbarActivity.outfitsFragment.hemisphereSelected();
GlobalVariables.getShared().setHasAskedForHemisphere(true);
}
});
builder.setCancelable(false);
//Create the AlertDialog object and return it
return builder.create();
}
And this is how it's displayed: 这是它的显示方式:
new HemisphereDialogFragment().show(getSupportFragmentManager(), "hemisphereDialog");
Another small side question, is there a way to change the text size for the items in the dialog? 另一个小问题是,有没有办法更改对话框中各项的文本大小?
您将警报对话框的setCancelable(false)
设置为false,但是您的片段仍设置为cancelable,您还需要将setCancelable(false)
添加到您的片段中。
您应该在Dialog片段本身而不是AlertDialog.Builder中调用setCancellable(false)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.