如何制作可多次使用的确认框? Android的

时间:2014-02-09 22:59:23

标签: android

所以我查看了互联网上的20多个问题和帖子,看不到在必须启用的代码之外制作确认框的方法,或者如果代码在其中,它会破坏它之外的所有变量...

这是一次尝试:

确认课程:

public class Confirm_Delete extends DialogFragment {
    public interface Confirm_Delete_Listener {
        public void onDialogPositiveClick(DialogFragment dialog);

        public void onDialogNegativeClick(DialogFragment dialog);
    }

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 = (Confirm_Delete_Listener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement Confirm_Delete_Listener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the Builder class for convenient dialog construction
        // 1. Instantiate an AlertDialog.Builder with its constructor
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // 2. Chain together various setter methods to set the dialog
        // characteristics
        builder.setMessage("Are you sure you want to delete?")
                .setTitle("Confirm")
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // Send the positive button event back to host
                                // activity
                                mListener
                                        .onDialogPositiveClick(Confirm_Delete.this);
                            }
                        })
                .setNegativeButton("cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                                // Send the negative button event back to the
                                // host activity
                                mListener
                                        .onDialogNegativeClick(Confirm_Delete.this);
                            }
                        });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

活动文件:(我无法将对话框片段实现到我的活动中,因为它正在实现onClickListener

// call delete
        if (v == delete_w1) {
DialogFragment newFragment = new Confirm_Delete();
            newFragment.show(getFragmentManager(), "missiles");

}


public class Confirmation extends FragmentActivity implements
            Confirm_Delete.Confirm_Delete_Listener {
        public void showNoticeDialog() {
            // Create an instance of the dialog fragment and show it
            DialogFragment dialog = new Confirm_Delete();
            dialog.show(getSupportFragmentManager(), "Confirm_Delete");
        }

        // 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) {

        }

3 个答案:

答案 0 :(得分:1)

步骤1:删除所有出现的confirm

步骤2:传入delete_confirm()某种侦听器对象,在其上调用某种方法来提供使用该对话框的用户的结果。您可以使用两个onClick()方法在此对象上调用方法。或者,在delete_confirm()方法中输入一对DialogInterface.OnClickListener对象,每个对象用于正面和负面按钮。

请注意create()会返回已配置AlertDialog的实例。该对话框仍需要通过DialogFragment显示。即使您告诉DialogFragment显示对话框,在您将主应用程序线程的控制权返回给Android之后的某个时间,对话框也不会实际显示。 Android中没有“阻塞对话框”模型,您可以调用某些方法并在用户响应输入之后停止执行。 Android中的一切都是事件驱动的;您需要在发生点击事件时处理它们的结果。

答案 1 :(得分:1)

我不确定,但我认为你是如何创建对话框的。

您应该为Dialog创建一个新类,然后,从您想要打开Dialog的任何位置创建该类的实例并显示它。

取自http://developer.android.com/guide/topics/ui/dialogs.html

的示例
public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        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) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

现在,正如同一页面在示例后面所说:“现在,当您创建此类的实例并在该对象上调用show()时”

接下来,从那里打开Dialog,您应该执行以下操作:

DialogFragment newFragment = FireMissilesDialogFragment();
newFragment.show(getFragmentManager(), "dialog");

更多信息,您应该阅读(可能已经阅读):

Android对DialogFragment的引用:http://developer.android.com/reference/android/app/DialogFragment.html

答案 2 :(得分:0)

为什么你的方法需要一个布尔参数?在第二行代码中,当您将其设置为false时,忽略已经传递的任何内容。

private boolean delete_confirm() {
        boolean confirm = false;
        // confirm delete
        Builder confirmation = new AlertDialog.Builder(this);
                // set message, title
        confirmation.setTitle("Delete");
        confirmation.setMessage("Do you want to Delete");
        confirmation.setPositiveButton("Delete",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        dialog.dismiss();
                        confirm = true;

                    }
                 });

        confirmation.setNegativeButton("cancel",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        confirm = false;

                    }
                });

        confirmation.create();
        confirmation.show();
        return confirm;