小编典典

单击“中性”按钮后能否保持对话框打开?

java

我有一个带有3个EditText的对话框,可用来获取ftp地址,用户名和密码。我使用.setNeutralButton创建一个“测试连接”按钮。我设法将其连接到ftp并显示结果吐司,但我不希望“测试按钮”关闭对话框。在连接测试期间如何保持对话框打开?

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();
    }
});

阅读 168

收藏
2020-11-30

共1个答案

小编典典

据我所知,不扩展Dialog类是不可能的。但是,使用您拥有的功能,将其单独放置Activity并使用可能会更容易,更好Dialogtheme。您所要做的就是Activity为此将代码放入新代码中,并在manifest使用中dialog theme

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

这将使一段Dialog时间的外观和感觉独立存在Activity

这是扩展Dialog的答案。我没有仔细研究过所有内容,但是如果您选择此选项,它可能会满足您的需求。

2020-11-30