如何显示顺序对话框弹出窗口?

时间:2014-02-16 03:37:21

标签: android popup

我在显示多个弹出窗口时遇到了一些麻烦。现在我有一个AlertDialog,弹出一个EditView,让用户输入他们想要制作的文件的名称,然后我将它传递给一个File对象然后一个Writer和一个新的Dialog应该弹出询问用户是否要启动音乐播放器。

然而,就像现在的情况一样,在第一个AlertDialog上按'确定'后,绝对没有任何反应。我不确定我做错了什么。有帮助吗?这是我的代码。

    //naming the playlist
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Exporting Playlist");
    alert.setMessage("Enter the name of the playlist!");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
            name = input.getText().toString() + ".m3u";
            popup = true;
      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
      }
    });

    alert.show();


    //after the playlist is named, put songs into file
    if (popup){
        popup = false;
        final File list = new File(mp3folderPath + name);
        FileWriter writer;
        BufferedWriter write;

        ArrayList<String> playlist = new ArrayList<String>();
        Log.d("poo", "mAdapter count: "+mAdapter.getCount());
        for (int i=0; i < mAdapter.getCount(); i++) {
            playlist.add(mAdapter.getItem(i));
        }
        Log.d("poo", playlist.toString());

        //write the songs  to the m3u playlist
        writer = new FileWriter(list);
        write = new BufferedWriter(writer);
        for (int i = 0; i<playlist.size(); i++){
            String[] name = playlist.get(i).split(" : ");
            Log.d("poo", name[0]);
            write.append(name[0]+"\n");
        }
        write.close();

        //popup window
        CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Exported playlist!");
        builder.setItems(choices, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
                    startActivity(intent);
                    finish();
                }
                else {
                    finish();
                }
            }
        });
    builder.show();
    }
}

2 个答案:

答案 0 :(得分:2)

AlertDialog.show()不会等待对话框消失。它会立即返回。这意味着用户做出选择后所做的所有逻辑必须进入对话框正面按钮的onClick功能。

基本上,if(弹出)代码中的所有内容都需要在onClick处理程序

答案 1 :(得分:2)

要显示顺序弹出窗口,连续弹出窗口的条件和代码必须可以从一个弹出窗口到另一个窗口。
AlertDialog1必须包含显示AlertDialog2 ...

的代码

尝试这样的事情:

    //naming the playlist
                AlertDialog.Builder alert = new AlertDialog.Builder(this);

                alert.setTitle("Exporting Playlist");
                alert.setMessage("Enter the name of the playlist!");

                // Set an EditText view to get user input 
                final EditText input = new EditText(this);
                alert.setView(input);

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //check if the name is not null
                        name = input.getText().toString() + ".m3u";
                    //Now instead of popup = true;
                 //call func to name the playlist and next dialog
                        callNextDialog();
                  }
                });

                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                  }
                });

                alert.show();


                //after the playlist is named, put songs into file
        //      if (popup){
        //          popup = false;
        //          }

                  public void callNextDialog(){
final File list = new File(mp3folderPath + name);
                    FileWriter writer;
                    BufferedWriter write;

                    ArrayList<String> playlist = new ArrayList<String>();
                    Log.d("poo", "mAdapter count: "+mAdapter.getCount());
                    for (int i=0; i < mAdapter.getCount(); i++) {
                        playlist.add(mAdapter.getItem(i));
                    }
                    Log.d("poo", playlist.toString());

                    //write the songs  to the m3u playlist
                    writer = new FileWriter(list);
                    write = new BufferedWriter(writer);
                    for (int i = 0; i<playlist.size(); i++){
                        String[] name = playlist.get(i).split(" : ");
                        Log.d("poo", name[0]);
                        write.append(name[0]+"\n");
                        write.close();
                      //popup window
            CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};

                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Exported playlist!");
                        builder.setItems(choices, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (which == 0) {
                        Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
                                    startActivity(intent);
                                    finish();
                                }
                                else {
                                    finish();
                                }
                            }
                        });
                    builder.show();
                  }
                }