是/否对话框和生命周期

时间:2011-07-21 02:39:46

标签: android dialog onclick lifecycle

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

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中再次获取( ),取决于反应。

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

我可以在哪里读到这个?当然,互联网上有很多关于此的信息,但对于我来说,作为一个相对新手,就像从消防水带中喝酒一样。

3 个答案:

答案 0 :(得分:2)

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

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();
}

答案 1 :(得分:1)

你可以这样做

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();
}

答案 2 :(得分:0)

您可以使用侦听器来实现此目的。就像在android文档中说的那样:

  1. 定义一个界面,其中包含您需要支持的操作(onDialogPositiveClickonDialogNegativeClick)。

    公共类NoticeDialogFragment扩展了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. 使显示对话框的类实现您的界面。

    公共类MainActivity扩展了FragmentActivity                           实现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. 让对话框在正确的时刻调用这些方法(当检测到setPositiveButtonsetNegativeButton点击时)。

    public class NoticeDialogFragment扩展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();
    }
    

    }

  4. 参考http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents