alaert对话框立刻消失了

时间:2016-08-12 05:15:59

标签: java android

当电子邮件和密码为空时,我创建了一个带有确定按钮的AlertDialog框。但AlertDialog框确实立即消失。我希望它在单击确定按钮后消失并保持此loginactivity.java。我怎样才能做到这一点?请帮帮我。

这是我的代码Loginactivity.java

mEmailSignInButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            my_username = mEmailView.getText().toString();
            my_password = mPasswordView.getText().toString();
           // Log.v("Username", "Username is " + my_username);

            savePreferences("Login onClick storedName", mEmailView.getText().toString());
            savePreferences("Login onClick storedPass", mPasswordView.getText().toString());

            if (my_username.isEmpty() || my_password.isEmpty() ) {

                Log.v("Login button onClick", "Login onClick 2 input are empty ");


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Click yes to exit!")
                        .setCancelable(false)
                        .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                LoginActivity.this.finish();
                            }
                        })
                        .setNegativeButton("No",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();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);

            }else{

                Log.v("Login button onClick", "Login onClick 2 input are not empty ");

                Bundle b = new Bundle();
                Intent intent= new Intent(LoginActivity.this, MainActivity.class);
                b.putString("Username", my_username);
                b.putString("Password", my_password);
                b.putString("flag", "add");
                intent.putExtras(b);
                startActivity(intent);

                // Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                // startActivity(i);
            }

5 个答案:

答案 0 :(得分:1)

显示对话框后,您正在启动活动

$sql = "EXPLAIN select created_at, user_id, power, sum(btc) Earnings FROM btcs GROUP BY user_id ORDER BY user_id";

$result = $conn->query($sql);

if (!$conn->error) {
   printf("Errormessage: %s\n", $conn->error);   // Check if your SQL has any syntax error
}

删除此行并运行。

答案 1 :(得分:0)

在您的代码中,首先使用构建器构建对话框,然后显示它。那就对了。但是,为什么要再次开始活动?没有必要这样做!这也是消失警报对话框的原因。启动新活动时,会自动关闭警报对话框!

所以你只需要删除这些行:

Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);

显示对话框不会保留当前活动

答案 2 :(得分:0)

 LoginActivity.this.finish() you have to replace this line with "dialog.dismiss()" 

答案 3 :(得分:0)

您在对话框显示后启动代码,因此警报对话框消失,将startActivityCode置于ok或取消按钮的警告对话框如下代码:

mEmailSignInButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            my_username = mEmailView.getText().toString();
            my_password = mPasswordView.getText().toString();
           // Log.v("Username", "Username is " + my_username);

            savePreferences("Login onClick storedName", mEmailView.getText().toString());
            savePreferences("Login onClick storedPass", mPasswordView.getText().toString());

            if (my_username.isEmpty() || my_password.isEmpty() ) {

                Log.v("Login button onClick", "Login onClick 2 input are empty ");


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Click yes to exit!")
                        .setCancelable(false)
                        .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, close
                                // current activity
                                LoginActivity.this.finish();
                            }
                        })
                        .setNegativeButton("No",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing

                Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);
                                dialog.cancel();

                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();


            }else{

                Log.v("Login button onClick", "Login onClick 2 input are not empty ");

                Bundle b = new Bundle();
                Intent intent= new Intent(LoginActivity.this, MainActivity.class);
                b.putString("Username", my_username);
                b.putString("Password", my_password);
                b.putString("flag", "add");
                intent.putExtras(b);
                startActivity(intent);

                // Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                // startActivity(i);
            }

答案 4 :(得分:0)

由于这段代码:

Intent i = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(i);