是/否对话框和生命周期

发布于 2024-11-25 11:44:11 字数 1446 浏览 1 评论 0原文

我确信这是一个基本问题,但我的研究没有产生任何有用的结果。我的新应用程序在某些情况下需要使用“是/否”对话框,但我不知道对话框如何适应应用程序生命周期。例如,我想创建一个方法来支持这种类型的构造:

if (yesNoAlert("Title", "Do you want to try again?") == true) {
   action1();
} else {
   action2();
}

该方法看起来像这样:

private boolean yesNoAlert(String title, String message) {
    final boolean returnValue;

    DialogInterface.OnClickListener dialogClickListener = new
                       DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                returnValue = true;
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                returnValue=false;
                break;
            }
        }
    };

    alertbox = new AlertDialog.Builder(this);
    alertbox.setMessage(message)
            .setTitle(title)
            .setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener)
            .show();
}

...但正如你所看到的,它还没有完成 - 有很多事情不太正确。我缺少的部分是如何知道对话框已完成...可以使用什么方法来使应用程序能够识别按钮已被按下的事实?当然,BUTTON_POSITIVE 和 BUTTON_NEGATIVE 操作会对此做出响应,但我的问题是如何使用指示器返回,以便等待响应的代码将在 action1() 或 action2( ),取决于响应。

目前,我没有看到我的应用程序有任何方法来确定这一点,甚至也没有看到从该代码创建方法/函数的有效方法。所以我错过了生命周期中的一些重要部分。

我可以在哪里读到这方面的内容?当然,互联网上有大量关于这方面的信息,但对于我作为一个相对新手来说,这就像尝试从消防水龙带中喝水一样。

I'm sure this is a fundamental question, but my research yields nothing useful. My new application needs to use a Yes/No dialog under a few circumstances, and I'm not getting how dialogs fit into the application lifecycle. For example, I would like to create a method to support this type of construct:

if (yesNoAlert("Title", "Do you want to try again?") == true) {
   action1();
} else {
   action2();
}

The method would look something like this:

private boolean yesNoAlert(String title, String message) {
    final boolean returnValue;

    DialogInterface.OnClickListener dialogClickListener = new
                       DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                returnValue = true;
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                returnValue=false;
                break;
            }
        }
    };

    alertbox = new AlertDialog.Builder(this);
    alertbox.setMessage(message)
            .setTitle(title)
            .setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener)
            .show();
}

... but as you can see, it is not finished - there are a number of things not quiet correct about it. The piece I'm missing is how to know that the dialog has finished... what method exists that can be utilized so that the application can pick up on the fact that the button has been pressed? Of course, the BUTTON_POSITIVE and BUTTON_NEGATIVE actions respond to that, but my question is how to return with an indicator, so that the code that's waiting for a response will pick up again at action1() or action2(), depending upon response.

At present, I do not see any way for my application to determine this, - nor even a valid way to make a method/function from that code. so I'm missing some vital piece from the lifecycle.

Where might I read up on this? Of course, there are volumes of information available on internet about this, but for me as a relative newbie it's like trying drink from a fire hose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉生梦死 2024-12-02 11:44:11

这将使需要采取的行动变得动态:

private Thread actionToDo;

private void yesNoAlert(String title, String message)
{
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
    {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                switch (which)
                {
                    case DialogInterface.BUTTON_POSITIVE:
                    actionToDo.start();
                    break;

                    case DialogInterface.BUTTON_NEGATIVE:
                    break;
                }
            }
    };
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).setTitle(title).show();
}

This will make the action that needs to be taken dynamic:

private Thread actionToDo;

private void yesNoAlert(String title, String message)
{
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
    {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                switch (which)
                {
                    case DialogInterface.BUTTON_POSITIVE:
                    actionToDo.start();
                    break;

                    case DialogInterface.BUTTON_NEGATIVE:
                    break;
                }
            }
    };
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(message).setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).setTitle(title).show();
}
余生共白头 2024-12-02 11:44:11

你可以这样做

private boolean yesNoAlert(String title, String message) {
    new AlertDialog.Builder(this).setMessage(message)
        .setTitle(title)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) { action1(); }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) { action2(); }
        })
        .show();
}

You could do like this

private boolean yesNoAlert(String title, String message) {
    new AlertDialog.Builder(this).setMessage(message)
        .setTitle(title)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) { action1(); }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) { action2(); }
        })
        .show();
}
喜爱皱眉﹌ 2024-12-02 11:44:11

您可以使用侦听器来实现此目的。正如 Android 文档中所述:

  1. 使用您需要支持的操作定义一个界面(onDialogPositiveClickonDialogNegativeClick)。

    公共类NoticeDialogFragment扩展DialogFragment {

    /* 创建此对话框片段实例的活动必须
     * 实现此接口以便接收事件回调。
     * 每个方法都会传递 DialogFragment,以防主机需要查询它。 */
    公共接口NoticeDialogListener {
        公共无效 onDialogPositiveClick(DialogFragment 对话框);
        公共无效 onDialogNegativeClick(DialogFragment 对话框);
    }
    
    // 使用该接口实例来传递操作事件
    注意对话框监听器 mListener;
    
    // 重写Fragment.onAttach()方法来实例化NoticeDialogListener
    @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        // 验证宿主Activity是否实现了回调接口
        尝试 {
            // 实例化NoticeDialogListener,以便我们可以将事件发送到主机
            mListener = (NoticeDialogListener) 活动;
        } catch (ClassCastException e) {
            // Activity没有实现接口,抛出异常
            抛出新的 ClassCastException(activity.toString()
                    +“必须实现NoticeDialogListener”);
        }
    }
    ...
    

    }

  2. 使显示对话框的类实现您的界面。

    公共类MainActivity扩展FragmentActivity
    实现NoticeDialogFragment.NoticeDialogListener{
    ...

    public void showNoticeDialog() {
        // 创建对话框片段的实例并显示它
        DialogFragment 对话框 = newNoticeDialogFragment();
        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
    }
    
    // 对话框片段通过以下方式接收对此 Activity 的引用
    // Fragment.onAttach() 回调,用于调用以下方法
    // 由NoticeDialogFragment.NoticeDialogListener接口定义
    @覆盖
    公共无效onDialogPositiveClick(DialogFragment对话框){
        // 用户触摸了对话框的肯定按钮
        ...
    }
    
    @覆盖
    公共无效onDialogNegativeClick(DialogFragment对话框){
        // 用户触摸了对话框的否定按钮
        ...
    }
    

    }

  3. 让您的对话框在正确的时刻调用这些方法(当检测到 setPositiveButtonsetNegativeButton 点击时)。

    公共类NoticeDialogFragment扩展DialogFragment {
    ...

    <前><代码>@Override
    公共对话框 onCreateDialog(Bundle savingInstanceState) {
    // 构建对话框并设置按钮单击处理程序
    AlertDialog.Builder 构建器 = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialog_fire_missiles)
    .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
    公共无效onClick(DialogInterface对话框,int id){
    // 将肯定按钮事件发送回宿主活动
    mListener.onDialogPositiveClick(NoticeDialogFragment.this);
    }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    公共无效onClick(DialogInterface对话框,int id){
    // 将否定按钮事件发送回宿主活动
    mListener.onDialogNegativeClick(NoticeDialogFragment.this);
    }
    });
    返回 builder.create();
    }

    }

参考 http://developer.android.com/guide/主题/ui/dialogs.html#PassingEvents

you can use a listener to achieve this. Like said in android documentation:

  1. Define a interface with the actions you need to support (onDialogPositiveClick and onDialogNegativeClick).

    public class NoticeDialogFragment extends DialogFragment {

    /* 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 NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }
    
    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;
    
    // 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 = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
    ...
    

    }

  2. Make the class that displays the dialog implements your interface.

    public class MainActivity extends FragmentActivity
    implements NoticeDialogFragment.NoticeDialogListener{
    ...

    public void showNoticeDialog() {
        // Create an instance of the dialog fragment and show it
        DialogFragment dialog = new NoticeDialogFragment();
        dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
    }
    
    // The dialog fragment receives a reference to this Activity through the
    // Fragment.onAttach() callback, which it uses to call the following methods
    // defined by the NoticeDialogFragment.NoticeDialogListener interface
    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // User touched the dialog's positive button
        ...
    }
    
    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User touched the dialog's negative button
        ...
    }
    

    }

  3. Make your dialog invoke these methods on the correct moment (when detect setPositiveButton or setNegativeButton click).

    public class NoticeDialogFragment extends DialogFragment {
    ...

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the positive button event back to the host activity
                       mListener.onDialogPositiveClick(NoticeDialogFragment.this);
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Send the negative button event back to the host activity
                       mListener.onDialogNegativeClick(NoticeDialogFragment.this);
                   }
               });
        return builder.create();
    }
    

    }

Ref http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文