如何从新活动中访问alertDialog信息?

时间:2017-08-04 06:37:44

标签: android alertdialog

我有一个应用程序,要求用户输入Heads或Tails:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Heads or Tails?")
                        .setSingleChoiceItems(coinOptions, 0, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                //start coin toss
                                startPlay(which, coins);
                            }
                        });
                AlertDialog ad = builder.create();
                ad.show();

我只是不确定它传递到新页面的确切内容?它是一个整数,0还是1或其他什么?我将其传递如下:

private void startPlay(int coinOption, int coins)
    {
        //start cointoss
        Intent playIntent = new Intent(this, CoinToss.class);
        playIntent.putExtra("bet", coinOption);
        playIntent.putExtra("coins", coins);
        this.startActivity(playIntent);
    }

并收到它:

Bundle extras = getIntent().getExtras();
        int totalCoins = extras.getInt("coins", -1);
        int bet = extras.getInt("bet", -1);

我想要告诉我头部或尾部的信息应该在变量“下注”下。

1 个答案:

答案 0 :(得分:1)

您的实施应该是这样的

  String [] coinOptions = {"Heads", "Tails"}; // adding your coin options

你的警报dailog应该是这样的

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Heads or Tails?")
                        .setSingleChoiceItems(coinOptions, -1, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                //start coin toss

                                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                                // Do something useful withe the position of the selected option
                                startPlay(selectedPosition , coins);
                            }
                        });
                AlertDialog ad = builder.create();
                ad.show();

然后你的开始游戏应该是这样的

private void startPlay(int coinOptionSelected, int coins)
    {
        //start cointoss
        Intent playIntent = new Intent(this, CoinToss.class);

          String selectedValue =null;
          if(coinOptionSelected != -1)     // if -1 means no option seletced do your rest handlings with else block
          {
                  selectedValue = coinOptions[coinOptionSelected];  //  if coinOptionSelected =1 then it will give Heads and if coinOptionSelected = 1 then it will give Tails
          }


        playIntent.putExtra("bet", selectedValue);
        playIntent.putExtra("coins", coins);  //  i dont know what this value you have
        this.startActivity(playIntent);
    }

最后你的接收应该是这样的

        Bundle extras = getIntent().getExtras();
        String selectedValue = extras.getString("bet");  // it will return Heads or Tails 
        int bet = extras.getInt("coins", -1);     // i dont know what this value you have