【问题标题】:how to pass yes/no value from alertdialog to an Activity in android如何将警报对话框中的是/否值传递给android中的活动
【发布时间】:2015-04-18 14:51:25
【问题描述】:

我最近开始在 android 中进行开发,目前卡在我需要从对话框接收值的地方。我有一个扩展 fragmentActivity 和一个 AlertDialog 类的 mainActivity。

1)我在 AlertDialog 类中创建了一个静态方法 showDefalutDialog,并从 mainActivity 按钮单击侦听器调用它,并将参数传递给 alertDialog。

2) 在 showDefalutDialog 静态方法中,我分别创建了带有 Yes/No DialogInterface 的 .setPositivebutton 和 .setNegativeButton。

现在这就是我想要做的。

1)当点击界面上的yes按钮时,它应该返回一个值给mainActivity 所以我可以在 if 语句中实现它来执行某个功能。

从 windows c# 编程这样做不是问题,但我只是不知道如何在 android 中实现它下面是相关的代码片段

  private void sendSms()
{

     SharedPreferences pref = getApplicationContext().getSharedPreferences("Sms_MyPref", 0);
        mail = pref.getString("email", null); // getting String
        tel = pref.getString("receiver_tel", null); // getting String
        layout = (LinearLayout)findViewById(R.id.linearLayout1);
        from_dateEdit = (EditText) findViewById(R.id.date_edit);
        to_dateEdit = (EditText) findViewById(R.id.date_edit_to);
        snButton = (Button)findViewById(R.id.form_send_button);
        from = (Button)findViewById(R.id.from);
        to = (Button)findViewById(R.id.to);



        spn = (Spinner)findViewById(R.id.form_spinner);
        spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                Object item = parent.getItemAtPosition(pos);
                spinnerV = (String) item;   

                if(pos == 0)
                {
                     layout.setVisibility( pos == 0 ? View.VISIBLE : View.VISIBLE);

                    from_dateEdit.setText(DatePickerFragment.getYesteesDate()); 
                    from.setOnClickListener(new Button.OnClickListener(){
                        @Override
                        public void onClick(View v) {     
                            // TODO Auto-generated method stub
                            showDatePicker();    
                        }
                    });

                    to_dateEdit.setText(DatePickerFragment.getTodaysDate());
                    to.setOnClickListener(new Button.OnClickListener(){
                        @Override
                        public void onClick(View v) {     
                            // TODO Auto-generated method stub
                            showDatePicker2();    
                        }
                    });

                 new1 = null;
                 new2 = null;
                    from_dateEdit.setText(new1);
                 to_dateEdit.setText(new2);


                }
                else if(pos == 1)
                {
                     layout.setVisibility( pos == 1 ? View.GONE : View.VISIBLE);
                     new1 = null;
                     new2 = null;
                     new1 = "a";
                     new2 = "b";
                }
                else if(pos == 2)
                {
                     layout.setVisibility( pos == 2 ? View.GONE : View.VISIBLE);
                     new1 = null;
                     new2 = null;
                     new1 = "a";
                     new2 = "b";
                }
                else if(pos == 3)
                {
                     layout.setVisibility( pos == 3 ? View.GONE : View.VISIBLE);
                     new1 = null;
                     new2 = null;
                     new1 = "a";
                     new2 = "b";
                }


            }
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });



        snButton.setOnClickListener(new OnClickListener() {

        public void onClick(View view)
          {
              if(new1 == null && new2 == null)
              {
                alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date range", false);
              }
                else if(new1 != null && new2 == null)
                  {
                    alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date TO", false);
                  }
                else if(new1 == null && new2 != null)
                      {
                        alert.showAlertDialog(MainActivity.this, "Error..", "Please specify a date FROM", false);
                      }


              else
              {
                gen = new1.toString()+","+new2.toString();
                alert();

           //i want to return a value from dialog yes/no click

            if(/*dialog yes is clicked*/)
            {

                 sms();
            }
            else if(/*dialog No is clicked*/)
            {
                return;
            }


              }

        }
        });


      } 

    private void alert()
{
    AlertDialogManager.showDefalutDialog(getApplicationContext(), spinnerV, mail, new1,new2);
}

public void sms()
{
    String both = "{"+ spinnerV.toString() + ","+gen.toString()+","+ mail.toString()+"}";

    sendSMS(tel,both);
}

和来自 AlertDialog 类的 showDefaultDialog 静态方法

    @SuppressLint("InflateParams")
public static void showDefalutDialog(final Context context, String order, final String mail, String fromD, String toD) {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
        // set title
        alertDialogBuilder.setTitle(R.string.finalmsg); 

        LayoutInflater li = LayoutInflater.from(context);
        View view = li.inflate(R.layout.data_summary_view, null);

        EditText EMAIL = (EditText)view.findViewById(R.id.Email);
        EditText Selectedorder = (EditText)view.findViewById(R.id.order);
        EditText Dfrom = (EditText)view.findViewById(R.id.edit_from);
        EditText Dto= (EditText)view.findViewById(R.id.edit_to);
        LinearLayout ll = (LinearLayout) view.findViewById(R.id.datelayout);
        LinearLayout l2 = (LinearLayout) view.findViewById(R.id.datelayout2);

        Selectedorder.setText(order);
        EMAIL.setText(mail);
        if(fromD.toString() != "a" && toD.toString() != "b")
        {
            ll.setVisibility(View.VISIBLE);
            l2.setVisibility(View.VISIBLE);
            Dfrom.setText(fromD);
            Dto.setText(toD);
        }
        else if(fromD.toString() == "a" && toD.toString() == "b")
        {
            ll.setVisibility(View.GONE);
            l2.setVisibility(View.GONE);


        }
        // set dialog message
        alertDialogBuilder.setView(view);
        //int msdt = data.toString().toCharArray().length;
        //Toast.makeText(context, "MsData char count : " + msdt , Toast.LENGTH_SHORT).show();;
        alertDialogBuilder
            .setOnCancelListener(new OnCancelListener() {               
                @Override
                public void onCancel(DialogInterface dialog) {
                    try {
                        Intent main = new Intent(context, MainActivity.class);
                        main.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 
                        context.startActivity(main);
                    } catch (Exception e) {
                        Log.d(TAG, "Error while starting Main activity from Dialog ! ");
                    }
                }
            })
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {        
                    Toast.makeText(context,"Your Order will be sent to "+ mail +" please  check your inbox for comfirmation." , Toast.LENGTH_SHORT).show(); 

                    dialog.cancel();


                }


              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    dialog.dismiss();
                }
            }); 

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create(); 
            // show it
            alertDialog.show();         
}

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以定义与此类似的自定义界面:

    public interface MyDialogClickListener {
        void onPositiveClicked(String value);
    }
    

    然后您创建实例并传递给方法,在该方法中创建对话框:

    public static void showDeafultDialog(..., MyDialogClickListener listener) {
        // ...
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
    
                listener.onPositiveClicked("you can pass yout value here")
            }
        })
        // ...
    }
    

    处理结果:

    private void sendSms() {
        AlertDialogManager.showDeafultDialog(..., new MyDialogClickListener() {
            @Override
            public void onPositiveClicked(String value) {
              // do whatever you want with value
            }
        });
    

    【讨论】:

    • 如果我遵循您的代码并调试运行它,它会在 alertDialog.show();
    • 我设法解决了我的初始代码,尝试了我在 c# 中使用的一个技巧,它现在可以工作了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多