我有一个带有3个EditTexts的Dialog,用于获取ftp地址,用户名和密码。我使用.setNeutralButton来创建一个“测试连接”按钮。我得到它连接到ftp并显示结果的Toast,但我不希望测试按钮关闭对话框。如何在连接测试期间保持Dialog打开?
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();
}
});
答案 0 :(得分:3)
据我所知,如果不扩展Dialog
课程,就不可能。但是,通过您拥有的功能,将其置于自己的Activity
并使用Dialog theme
可能会更容易,更好。您所要做的就是将代码放到新的Activity
中,并manifest
使用dialog theme
<activity
android:name="com.your.package.YourClassName"
android:label="YOurLabel"
android:theme="@android:style/Theme.Dialog" >
</activity>
这将在Dialog
Activity
的外观。
这是SO answer on extending Dialog。我没有仔细研究过,但如果选择这个选项,它看起来可能会给你所需要的东西。