Is it possible to keep a Dialog open after clickin

2019-03-05 15:33发布

This question already has an answer here:

I have a Dialog with 3 EditTexts that I use to get an ftp address, username, and password. I used .setNeutralButton to create a button to "Test Connection". I got it working to connect to the ftp and show a Toast with the result but I don't want the Test Button to close the Dialog. How can I keep the Dialog open during the connection test?

livePreviewChk.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        LinearLayout lila1 = new LinearLayout(NewSite.this);
        lila1.setOrientation(1); // 1 is for vertical orientation

        final EditText serverName = new EditText(NewSite.this);
        serverName.setHint("Server name");

        final EditText serverAddress = new EditText(NewSite.this);
        serverAddress.setHint("Server Address");

        final EditText username = new EditText(NewSite.this);
        username.setHint("Username:");

        final EditText password = new EditText(NewSite.this);
        password.setHint("Password");

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(
                NewSite.this);
        alt_bld.setIcon(R.drawable.ftpicon);
        alt_bld.setTitle("Enter the login details for the host FTP")
                .setCancelable(true)
                .setPositiveButton("Save",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                }
                            }
                        })
                .setNeutralButton("Test Connection",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                FTPConnector testConnection = new FTPConnector();
                                boolean status = testConnection
                                        .ftpConnect(host, user, pass,
                                                port);
                                if (status == true) {
                                    connectionSuccessfull = true;
                                } else {
                                    connectionSuccessfull = false;
                                }
                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

        lila1.addView(serverName);
        lila1.addView(serverAddress);
        lila1.addView(username);
        lila1.addView(password);

        AlertDialog alert = alt_bld.create();
        alert.setView(lila1);
        alert.show();
    }
});

1条回答
仙女界的扛把子
2楼-- · 2019-03-05 16:13

From what I know, it is not possible without extending the Dialog class. However, with the functionality that you have it may be easier and better just to put it in its own Activity and use a Dialog theme. All you have to do is put your code into a new Activity for this and in your manifest use the dialog theme

<activity
        android:name="com.your.package.YourClassName"
        android:label="YOurLabel"
        android:theme="@android:style/Theme.Dialog" >
</activity>

This will give the look and feel of a Dialog while being contained in its own Activity

Here is a SO answer on extending Dialog. I haven't looked through it all but looks like it may give you what you need if you choose this option.

查看更多
登录 后发表回答