简体   繁体   中英

Alert dialog gets dismissed by itself?

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


    }

if the username is empty i just want to show a toast,but the dialog gets dismissedafter show the toast y?i want it to stay as it is

There is a work around you can implement. You just would need to re-show the dialog if whatever checks you're performing don't pass. Take a look at this previous answer: Is there a way prevent AlertDialog from closing with invalid inputs?

Note: You may not even need to implement your own alert dialog to do this. You should be able to do this right in your onClick() listener by calling dialog.show(). Give it a shot and let us know if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM