【问题标题】:how to use a class in an activity? DialogFragment [duplicate]如何在活动中使用类? DialogFragment [重复]
【发布时间】:2018-07-12 23:46:57
【问题描述】:

来自Google Developer UI guide 您可以通过扩展 DialogFragment 并在 onCreateDialog() 回调方法中创建 AlertDialog 来完成各种对话框设计——包括自定义布局和对话框设计指南中描述的那些。

例如,这是一个在 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);
}

【问题讨论】:

  • getSupportFragmentManager().show(new ResetAll()) 最有可能

标签: java android android-studio


【解决方案1】:
@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 和标签 对话框片段的名称。

您可以通过调用getSupportFragmentManager() 获取FragmentManager 来自FragmentActivitygetFragmentManager() 来自Fragment

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多