是否可以点击中间按钮后保持对话框打开?对话框、按钮

2023-09-04 04:04:10 作者:长腿欧巴

我有一个对话框,我用它来获取FTP地址,用户名和密码3 EditTexts。我用.setNeutralButton创建一个按钮测试连接。我得到它的工作连接到FTP,并显示其结果敬酒,但我不希望测试按钮,关闭对话框。我怎样才能连接测试期间保持对话框打开?

 现场previewChk.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图v){
        的LinearLayout lila1 =新的LinearLayout(NewSite.this);
        lila1.setOrientation(1); // 1为垂直取向

        最后的EditText的serverName =新的EditText(NewSite.this);
        serverName.setHint(服务器名称);

        最后的EditText serverAddress =新的EditText(NewSite.this);
        serverAddress.setHint(服务器地址);

        最后的EditText的用户名=新的EditText(NewSite.this);
        username.setHint(用户名:);

        最后的EditText密码=新的EditText(NewSite.this);
        password.setHint(密码);

        AlertDialog.Builder alt_bld =新AlertDialog.Builder(
                NewSite.this);
        alt_bld.setIcon(R.drawable.ftpicon);
        alt_bld.setTitle(输入登录信息主机FTP)
                .setCancelable(真)
                .setPositiveButton(保存,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话框,
                                    INT编号){
                                }
                            }
                        })
                .setNeutralButton(测试连接,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话框,
                                    INT编号){
                                FTPConnector testConnection =新FTPConnector();
                                布尔状态= testConnection
                                        .ftpConnect(主机,用户,传球,
                                                端口);
                                如果(状态==真){
                                    connectionSuccessfull = TRUE;
                                } 其他 {
                                    connectionSuccessfull = FALSE;
                                }
                            }
                        })
                .setNegativeButton(取消,
                        新DialogInterface.OnClickListener(){
                            公共无效的onClick(DialogInterface对话框,
                                    INT编号){
                                //如果单击此按钮,只需关闭
                                //对话框并做什么
                                dialog.cancel();
                            }
                        });

        lila1.addView(服务器名);
        lila1.addView(serverAddress);
        lila1.addView(用户名);
        lila1.addView(密码);

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

解决方案

据我所知,这也不是没有可能延长对话框类。然而,你有可能会更容易,更好的功能,只需把它放在自己的活动和使用对话主题。所有你需要做的就是把你的code到一个新的活动,这和你的清单使用对话主题

 <活动
        机器人:名称=com.your.package.YourClassName
        机器人:标签=YOurLabel
        机器人:主题=@安卓风格/ Theme.Dialog>
< /活性GT;
 

这将给外观和感觉的对话框同时包含在自己的活动

photoshop cs6怎么导出图片

下面是一个SO回答有关延长对话框。我还没有经历过这一切看起来,但看起来它可能会给你带来你所需要的,如果你选择了这个选项。

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

解决方案

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.

 
精彩推荐
图片推荐