I'm having some trouble with displaying multiple popups. Right now I have an AlertDialog that pops up with an EditView for the user to put in the name of the file they want to make, which I would then pass into a File object and then a Writer and a new Dialog is supposed to pop up asking the user if they want to launch the music player.
我在显示多个弹出窗口时遇到了一些麻烦。现在我有一个AlertDialog,弹出一个EditView,让用户输入他们想要制作的文件的名称,然后我将它传递给一个File对象然后一个Writer和一个新的Dialog应该弹出询问用户是否要启动音乐播放器。
However, as things are now, after I press 'Ok' on the first AlertDialog, absolutely nothing happens. I'm not sure what I'm doing wrong. Any help? Here is my code.
但是,就像现在的情况一样,在第一个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 个解决方案
#1
To show sequential popup, the conditions and code for consecutive popup(s) would have to be reachable from one to the other.
AlertDialog1 has to contain the code which would show AlertDialog2...
要显示顺序弹出窗口,连续弹出窗口的条件和代码必须可以从一个弹出窗口到另一个窗口。 AlertDialog1必须包含显示AlertDialog2的代码...
Try something like this:
尝试这样的事情:
//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(); } }
#2
AlertDialog.show() doesn't wait for the dialog to go away. It returns immediately. That means ALL of the logic of what to do after the user makes a choice has to go in the onClick function of the dialog's positive button.
AlertDialog.show()不会等待对话框消失。它会立即返回。这意味着用户做出选择后所做的所有逻辑必须进入对话框正面按钮的onClick功能。
Basically, everything in your if(popup) code needs to be in the onClick handler
基本上,if(弹出)代码中的所有内容都需要在onClick处理程序中
#1
To show sequential popup, the conditions and code for consecutive popup(s) would have to be reachable from one to the other.
AlertDialog1 has to contain the code which would show AlertDialog2...
要显示顺序弹出窗口,连续弹出窗口的条件和代码必须可以从一个弹出窗口到另一个窗口。 AlertDialog1必须包含显示AlertDialog2的代码...
Try something like this:
尝试这样的事情:
//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(); } }
#2
AlertDialog.show() doesn't wait for the dialog to go away. It returns immediately. That means ALL of the logic of what to do after the user makes a choice has to go in the onClick function of the dialog's positive button.
AlertDialog.show()不会等待对话框消失。它会立即返回。这意味着用户做出选择后所做的所有逻辑必须进入对话框正面按钮的onClick功能。
Basically, everything in your if(popup) code needs to be in the onClick handler
基本上,if(弹出)代码中的所有内容都需要在onClick处理程序中