gpt4 book ai didi

android - 旋转屏幕时重新创建 AlertDialog 的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 03:15:19 25 4
gpt4 key购买 nike

我目前正在使用 AlertDialog 在我的应用程序中创建一个简单的对话框。我使用的代码如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle(DialogTitle);
builder.setItems(R.array.options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
reset();
break;
case 1:
exit();
break;
default:
break;
}
}
});
builder.show();

我读到过,最好的选择可能是创建一个扩展 DialogFragment 的类,然后使用我的 DialogFragment 而不是我当前的实现。

任何人都可以确认这是最好的解决方案,提出更好的建议并可能给我一个例子吗?

谢谢。

最佳答案

public class ListDialogFragment extends DialogFragment 
{
// Use this instance of the interface to deliver action events
private listDialogListener mListener;
private String title;
private int items;

/**
* Create a new instance of EndGameDialogFragment, String dialogTitle
* and ing dialogListItems as an argument.
*/
static ListDialogFragment newInstance(String dialogTitle, int dialogListItems)
{
ListDialogFragment frag = new ListDialogFragment();

// Supply num input as an argument.
Bundle args = new Bundle();
args.putString("title", dialogTitle);
args.putInt("items", dialogListItems);
frag.setArguments(args);

return frag;
}


/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface listDialogListener
{
public void onDialogClick(DialogFragment dialog, int which);
}

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try
{
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (listDialogListener) activity;
}
catch (ClassCastException e)
{
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
title = getArguments().getString("title"); //retrive the titleString
items = getArguments().getInt("items"); //retrive array of items for the list (from strings.xml)

//build the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setItems(items, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
switch(which)
{
case 0:
mListener.onDialogClick(ListDialogFragment.this, which);
break;
case 1:
mListener.onDialogClick(ListDialogFragment.this, which);
break;
default:
break;
}
}
});
return builder.create();
}

}

关于android - 旋转屏幕时重新创建 AlertDialog 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com