来自Google Developer UI guide 通过扩展DialogFragment并在onCreateDialog()回调方法中创建AlertDialog,可以完成各种对话框设计,包括自定义布局和Dialogs设计指南中描述的布局。
例如,这是在DialogFragment中管理的基本AlertDialog:
什么?
public class ResetAll extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.reset)
.setPositiveButton(R.string.reset2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code
}
});
return builder.create();
}
}
在这里使用课程吗?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// USE HERE! DIALOG
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:0)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// USE HERE! DIALOG
ResetAll dialog = new ResetAll();
dialog.show(getSupportFragmentManager(), "restFragmentDialog");
// restFragmentDialog is a unique tag name that the system uses to save and restore the fragment state when necessary.
// The tag also allows you to get a handle to the fragment by calling findFragmentByTag().
}
return super.onOptionsItemSelected(item);
}
从有问题的Google UI link中摘录。
显示对话框
要显示对话框时,请创建一个实例
DialogFragment
并调用show()
,并传递FragmentManager
和一个标记 对话框片段的名称。您可以致电
FragmentManager
来获取getSupportFragmentManager()
来自FragmentActivity
或来自getFragmentManager()
的{{1}}。