【问题标题】:Alert dialog gets dismissed by itself?警报对话框会自行消失?
【发布时间】:2011-09-28 11:59:57
【问题描述】:
@Override
    protected Dialog onCreateDialog(int id) {

        LayoutInflater factory = LayoutInflater.from(GmailFetchActivity.this);

        final View textEntryView = factory.inflate(R.layout.alertdialog_gmail, null);

        final EditText username = (EditText) textEntryView.findViewById(R.id.edit_username);
        editxt_pass = (EditText) textEntryView.findViewById(R.id.edit_password);
        final CheckBox remember = (CheckBox) textEntryView.findViewById(R.id.checkBox_remember);

        username.setText(myPrefs.getString("username", ""));
        editxt_pass.setText(myPrefs.getString("password", ""));


        AlertDialog.Builder alert=new AlertDialog.Builder(this);

        alert.setTitle("Gmail login");      
        alert.setView(textEntryView);  

        alert.setPositiveButton("Login", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) {          

                if(username.getText().toString().trim().equalsIgnoreCase(""))
                {

                }
                else{
                if(isNetworkAvailable()){               

                    String user=username.getText().toString();
                    String pass=editxt_pass.getText().toString();

                    if(remember.isChecked()){

                        prefsEditor.putString("username",user);
                        prefsEditor.putString("password",pass);
                        prefsEditor.commit();
                    }
                    else{
                        prefsEditor.putString("username","");
                        prefsEditor.putString("password","");
                        prefsEditor.commit();
                    }

                    progressHrz.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    progressHrz.setMessage("Fetching attachment's list");
                    progressHrz.setCancelable(true);
                    progressHrz.show();

                    new FetchGmail().execute(user+"@gmail.com",pass);               
                }
                else
                    Toast.makeText(getApplicationContext(), "No internet access", Toast.LENGTH_LONG).show();
                }
            } 
        });
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 

                finish();
            } 
        }) ;

         return alert.create();


    }

如果用户名是空的,我只想显示一个 toast,但显示 toast 后对话框会被关闭?我希望它保持原样

【问题讨论】:

  • 这是警报对话框的默认行为。如果您单击正面按钮或任何按钮,它将自动关闭。如果您不希望这样做,请执行自定义对话框。

标签: android


【解决方案1】:

您可以实施一种解决方法。如果您执行的任何检查未通过,您只需要重新显示对话框。看看这个之前的回答:Is there a way prevent AlertDialog from closing with invalid inputs?

注意:您甚至可能不需要实现自己的警报对话框来执行此操作。您应该能够通过调用 dialog.show() 在 onClick() 侦听器中正确执行此操作。试一试,让我们知道它是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多