第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按下警報對話框按鈕總是返回 0 值

按下警報對話框按鈕總是返回 0 值

慕慕森 2021-12-01 16:00:58
因此,我想在彈出警報對話框時檢測用戶按下的按鈕。這是我的代碼。public class AlertUtils {    private int BTN_PRESSED;    private AlertDialog.Builder builder;    public AlertUtils(Context context){        builder = new AlertDialog.Builder(context);    }    public int ShowAlertWithTwoButtons(String Title,String Message,String PositiveButtonText,                                        String NegativeButtonText){        builder.setTitle(Title);        builder.setMessage(Message);        builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                BTN_PRESSED = i;            }        });        builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                BTN_PRESSED = i;                dialogInterface.dismiss();            }        });        builder.show();        return BTN_PRESSED;    }}通過調(diào)用ShowAlertWithTwoButtons方法,返回檢測正或負按鈕按下的 int 值。我的問題是當(dāng)我從警報對話框中選擇時它給了我默認的 0 值,當(dāng)我再次打開我們的警報對話框時,它返回正確的值。
查看完整描述

2 回答

?
繁花如伊

TA貢獻2012條經(jīng)驗 獲得超12個贊

您總能獲得BTN_PRESSED與0價值,只要你在你的實例化AlertUtils對象和調(diào)用ShowAlertWithTwoButtons方法。但是如果你ShowAlertWithTwoButtons再次回憶,你會得到另一個值。


我認為您目前正在做的事情如下:


// First, you're instantiating the object

AlertUtils alertUtils = new AlertUtils(getContext());


// then you're calling the method

int pressedButton = alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");


// which will return pressedButton as 0


// then you calling the method again after clicked yes or no

int anotherPressedButton = alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");


// which will not zero. But can be -1, -2, -3 like in the

// https://developer.android.com/reference/android/content/DialogInterface.html

由于AlertDialog 接口的異步特性,如果想在點擊后直接獲取按鈕值,這是不正確的。


相反,您需要向 AlertUtils 添加一個偵聽器(哦,不,另一個偵聽器)。


更新


您需要為單擊按鈕添加另一個偵聽器,如下所示:


public class AlertUtils {


    public interface Listener {

      void onButtonClicked(int pressedButton);

    }


    private Listener mListener;


    private AlertDialog.Builder builder;


    public AlertUtils(Context context, Listener listener){

        builder = new AlertDialog.Builder(context);

        mListener = listener;

    }


    public void ShowAlertWithTwoButtons(String Title,String Message,String PositiveButtonText,

                                        String NegativeButtonText){

        ...


        builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialogInterface, int i) {

                mListener.onButtonClicked(i);

            }

        });


        builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {

            @Override

            public void onClick(DialogInterface dialogInterface, int i) {

                mListener.onButtonClicked(i);

                dialogInterface.dismiss();

            }

        });


        builder.show();

    }

}

然后您可以使用以下方法創(chuàng)建和調(diào)用該方法:


// create the listener to listen for the clicked button.

AlertUtils.Listener listener = new AlertUtils.Listener() {

      @Override

      public void onButtonClicked(int pressedButton) {

        // here you'll receive the button value

        // do something here.

      }

   };


AlertUtils alertUtils = new AlertUtils(getContext(), listener);


// then you're calling the method

alertUtils.ShowAlertWithTwoButtons("title", "message", "yes", "no");


查看完整回答
反對 回復(fù) 2021-12-01
?
青春有我

TA貢獻1784條經(jīng)驗 獲得超8個贊

用這種方法試試。像這樣制作 AlertUtils 類。


public class AlertUtils {

private AlertDialog.Builder builder;

private AlertDialogListener alertDialogListener;


// Interface to send back the response of click

interface AlertDialogListener {

    void onClick(int a);

}


public AlertUtils(Context context, AlertDialogListener alertDialogListener) {

    builder = new AlertDialog.Builder(context);

    this.alertDialogListener = alertDialogListener;

}


public void ShowAlertWithTwoButtons(String Title, String Message, String PositiveButtonText,

                                    String NegativeButtonText) {

    builder.setTitle(Title);

    builder.setMessage(Message);

    builder.setPositiveButton(PositiveButtonText, new DialogInterface.OnClickListener() {

        @Override

        public void onClick(DialogInterface dialogInterface, int i) {

            // if you want to pass the actual value of i,then pass the i in onClick or if you want 1 on 

            // positive button click then pass 1 here.

            alertDialogListener.onClick(1);

        }

    });

    builder.setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {

        @Override

        public void onClick(DialogInterface dialogInterface, int i) {

            // if you want to pass the actual value of i, then pass the i in onClick or if you want 1 on 

            // negative button click then pass 0 here.

            alertDialogListener.onClick(0);

            dialogInterface.dismiss();

        }

    });

    builder.show();

}

}


在您需要的地方以這種方式調(diào)用對話框。


  AlertUtils alertUtils = new AlertUtils(getContext(), new AlertUtils.AlertDialogListener() {

        @Override

        public void onClick(int a) {

            if (a == 1) {

                // Do your work on Positive button click

            } else {

                // Do your work on Negative button click

            }

        }

    });

    alertUtils.ShowAlertWithTwoButtons("Alert Dialog", "Alert Dialog Description ", "Positive", "Negative");


查看完整回答
反對 回復(fù) 2021-12-01
  • 2 回答
  • 0 關(guān)注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號