如何在Android上将AlertDialog重复用于“是/否"? [英] How can I reuse an AlertDialog for Yes/No on Android?

查看:80
本文介绍了如何在Android上将AlertDialog重复用于“是/否"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重用显示自定义标题的对话框,然后将是/否"单击发送给启动对话框的功能.

I'm trying to find the way to reuse a Dialog that shows customized titles, then send the Yes/No click to the function that has launched the Dialog.

我有两个按钮,即保存"和关闭",都调用是/否"对话框,一个显示您是否要保存",另一个显示关闭更改?".

I have two buttoms, Save and Dismiss, and both call a Yes/No Dialog, one showing "Do you want to save" and the other "Dismiss changes?".

我认为我的过程非常肮脏",但我想它可以工作,但是我的问题是"View view"变量,我不知道如何将其从Activity传递到Dialog,所以我可以使用可以调用启动对话框的功能.

I think my procedure is very "dirty" but I guess it can work, but my problem is the "View view" variable, I don't know how to pass it from the Activity to the Dialog, so I can use it to recall the function that launched the Dialog.

预先感谢, 赫尼·赫兹(HerniHdez)

Thanks in advance, HerniHdez

.java我的活动(片段)

public void open_HH_Fragment_YesNo(View view, String aux_title, String aux_function)
{
    Bundle bundle=new Bundle();
    bundle.putString("setMessage", aux_title);
    bundle.putString("callingFunction", aux_function);

    DialogFragment newFragment = new HH_Fragment_YesNo();
    newFragment.setArguments(bundle);
    newFragment.show(getSupportFragmentManager(), "HH_Fragment_YesNo");
}

public void SaveChanges(View view, String aux_YesNo)
{
    if (aux_YesNo == "")
    {
        Toast.makeText(this, "Save changes?", Toast.LENGTH_SHORT).show();
        open_HH_Fragment_YesNo(view, "Save changes?", "SaveChanges");
    }
    else if (aux_YesNo == "Yes")
    {
        Toast.makeText(this, "Saving changes", Toast.LENGTH_SHORT).show();
    }
    else if (aux_YesNo == "No")
    {
        Toast.makeText(this, "Save Cancelled", Toast.LENGTH_SHORT).show();
    }
}

public void DismissChanges(View view, String aux_YesNo)
{
    if (aux_YesNo == "")
    {
        Toast.makeText(this, "Dismiss changes?", Toast.LENGTH_SHORT).show();
        open_HH_Fragment_YesNo(view, "Dismiss changes?", "DismissChanges");
    }
    else if (aux_YesNo == "Yes")
    {
        Toast.makeText(this, "Dismiss OK", Toast.LENGTH_SHORT).show();
        Close(view);
    }
    else if (aux_YesNo == "No")
    {
        Toast.makeText(this, "Dismiss Cancelled", Toast.LENGTH_SHORT).show();
    }
}

// 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 HH_Fragment_YesNo.YesNoDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_function)
{
    // User touched the dialog's positive button
    Toast.makeText(this, "User clicked on Yes", Toast.LENGTH_SHORT).show();

    if (aux_function == "SaveChanges")
    {
        SaveChanges(view, "Yes");
    }
    else if (aux_function == "DismissChanges")
    {
        DismissChanges(view, "Yes");
    }
}

@Override
public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_function)
{
    Toast.makeText(this, "User clicked on NO", Toast.LENGTH_SHORT).show();

    if (aux_function == "SaveChanges")
    {
        SaveChanges(view, "No");
    }
    else if (aux_function == "DismissChanges")
    {
        DismissChanges(view, "No");
    }
}

对话框的

.java(完整)

public class HH_Fragment_YesNo extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    String setMessage = getArguments().getString("setMessage");
    final String callingFunction = getArguments().getString("callingFuntion");

    builder
        .setMessage(setMessage)                                             // R.string.dialog_fire_missiles
        .setPositiveButton("Sí", new DialogInterface.OnClickListener()      // R.string.fire
        {
            public void onClick(DialogInterface dialog, int id)
            {
                // Exit without saving
                mListener.onDialogPositiveClick(HH_Fragment_YesNo.this, view, callingFunction);
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener()      // R.string.cancel
        {
            public void onClick(DialogInterface dialog, int id)
            {
                // User cancelled the dialog
                mListener.onDialogNegativeClick(HH_Fragment_YesNo.this, view, callingFunction);
            }
        });

    // Create the AlertDialog object and return it
    return builder.create();
}


/* 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 YesNoDialogListener
{
    public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_Function);
    public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_Function);
}


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

推荐答案

完整的解决方案

1)创建界面

import android.content.DialogInterface;

public interface AlertMagnatic {

    public abstract void PositiveMethod(DialogInterface dialog, int id);
    public abstract void NegativeMethod(DialogInterface dialog, int id);
}

2)概括确认对话框的方法.

public static void getConfirmDialog(Context mContext,String title, String msg, String positiveBtnCaption, String negativeBtnCaption, boolean isCancelable, final AlertMagnatic target) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

        int imageResource = android.R.drawable.ic_dialog_alert;
        Drawable image = mContext.getResources().getDrawable(imageResource);

        builder.setTitle(title).setMessage(msg).setIcon(image).setCancelable(false).setPositiveButton(positiveBtnCaption, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                target.PositiveMethod(dialog, id);
            }
        }).setNegativeButton(negativeBtnCaption, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                target.NegativeMethod(dialog, id);
            }
        });

        AlertDialog alert = builder.create();
        alert.setCancelable(isCancelable);
        alert.show();
        if (isCancelable) {
            alert.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    target.NegativeMethod(null, 0);
                }
            });
        }
    }

3)使用方法

getConfirmDialog(getString(R.string.logout), getString(R.string.logout_message), getString(R.string.yes), getString(R.string.no), false,
                new AlertMagnatic() {

                    @Override
                    public void PositiveMethod(final DialogInterface dialog, final int id) {}

                    @Override
                    public void NegativeMethod(DialogInterface dialog, int id) {

                    }
                });

这篇关于如何在Android上将AlertDialog重复用于“是/否"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆