如何在Android上重复使用AlertDialog for YesNo?

如何在Android上重复使用AlertDialog for YesNo?,第1张

概述我正在尝试找到重用显示自定义标题的Dialog的方法,然后将Yes / No click发送到已启动Dialog的功能. 我有两个按钮,保存和解除,并且都调用是/否对话,一个显示“你想保存”,另一个显示“关闭更改?”. 我认为我的程序非常“脏”但我想它可以工作,但我的问题是“查看视图”变量,我不知道如何将它从Activity传递给Dialog,所以我可以用它来回忆启动Dialog的功能. 提前致谢 我正在尝试找到重用显示自定义标题的Dialog的方法,然后将Yes / No click发送到已启动Dialog的功能.

我有两个按钮,保存和解除,并且都调用是/否对话,一个显示“你想保存”,另一个显示“关闭更改?”.

我认为我的程序非常“脏”但我想它可以工作,但我的问题是“查看视图”变量,我不知道如何将它从Activity传递给Dialog,所以我可以用它来回忆启动Dialog的功能.

提前致谢,
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,"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,"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@OverrIDepublic 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");    }}@OverrIDepublic voID onDialogNegativeClick(DialogFragment dialog,String aux_function){    Toast.makeText(this,"User clicked on NO","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,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,String aux_Function);    public voID onDialogNegativeClick(DialogFragment dialog,String aux_Function);}// Use this instance of the interface to deliver action eventsYesNoDialogListener mListener;// OverrIDe the Fragment.onAttach() method to instantiate the NoticeDialogListener@OverrIDepublic 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 for Yes / No?全部内容,希望文章能够帮你解决如何在Android上重复使用AlertDialog for Yes / No?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1145452.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存