Android 在整个应用程序中调用相同的警报对话框

Android call same alert dialog in the entire application

我在操作栏上有一个用于更改密码的菜单。下面是它的代码。我想放置此代码,以便我可以通过单击该菜单在我的应用程序中的任何位置调用相同的代码。有什么办法吗? --- ChangingPassword.java---

public void showDialog(final Context ctx, final String user_id, final String storedPass)
{

    LayoutInflater layoutInflater = LayoutInflater.from(ctx);
    View promptView = layoutInflater.inflate(R.layout.change_password, null);

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

    // set prompts.xml to be the layout file of the alertdialog builder
    alertDialogBuilder.setView(promptView);

    final EditText old_password = (EditText) promptView.findViewById(R.id.old_password);
    final EditText new_password = (EditText) promptView.findViewById(R.id.new_password);
    final EditText c_new_password = (EditText) promptView.findViewById(R.id.c_new_password);

    final int error_count;
    /*old_password.setTypeface(font);
    new_password.setTypeface(font);
    c_new_password.setTypeface(font);
    */
    final String old_pwd = old_password.getText().toString();
    final String new_pwd = new_password.getText().toString();
    final String c_new_pwd = c_new_password.getText().toString();
    // setup a dialog window
    alertDialogBuilder
            .setTitle("Change Login Password")
            .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                           if(old_pwd.equalsIgnoreCase("") || (old_pwd.equalsIgnoreCase(storedPass)))
                            {
                                old_password.setError(Html.fromHtml("<font color='red'>Enter a valid password</font>"));
                                old_password.requestFocus();
                            }

                            else if( new_pwd.equalsIgnoreCase(""))
                            {
                                new_password.setError(Html.fromHtml("<font color='red'>Enter a valid password</font>"));
                                new_password.requestFocus();
                            }

                            else if( c_new_pwd.equalsIgnoreCase(""))
                            {
                                c_new_password.setError(Html.fromHtml("<font color='red'>Enter a valid password</font>"));
                                c_new_password.requestFocus();
                            }

                            else if(!new_pwd.equals(c_new_pwd))
                            {
                                c_new_password.setError(Html.fromHtml("<font color='red'>Password & Confirm Passwords do not match</font>"));
                                c_new_password.requestFocus();
                            }

                            else
                            {
                                try {

                                    UserTask task = new UserTask();
                                    String result = task.execute(new String[] {"changePassword",user_id,new_pwd}).get();
                                    System.out.print(result);
                                }

                                catch (Exception e)
                                {
                                    Toast.makeText(ctx, ""+e.toString(), Toast.LENGTH_LONG).show();
                                }
                            }
                        }
                    })
            .setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });

            // create an alert dialog
            AlertDialog alertD = alertDialogBuilder.create();
            alertD.show();
            //return alertD;

}

---调用Activity--- new ChangePassword().showDialog(TeacherMain.this, user_id, 密码); return 真;

public class MyDialog{
    public void showDialog(Context ctx)
    {
        /* Create dialog here and do whatever you are doing right now in your method*/
        dialog.show();
    }
}

然后从您的 activity 类 拨打电话,如下所示

new MyDialog().showDialog(ActivityName.this);

对于您的检查部分,您必须删除嵌套的 if 语句

if(old_pwd.equalsIgnoreCase("")){
        //print: Enter old password                                 
}else if( new_pwd.equalsIgnoreCase("")){
       //print: Enter new password 
}else if( c_new_pwd.equalsIgnoreCase("")){
      //print: Enter c_new_pwd 
}else if(! new_pwd.equals(c_new_pwd)){
      //print password doesnot match     
}else

{
            try {

                UserTask task = new UserTask();
                String result = task.execute(new String[] {"changePass",user_id}).get();
                 System.out.print(result);
            }catch (Exception e){
                  Toast.makeText(TeacherMain.this, ""+e.toString(),Toast.LENGTH_LONG).show();
            }


}

而不是创建新的 AlertDialog 使用 alertDialogBuilder.show();