2015-04-04 73 views
1

我有一個操作欄上的菜單來更改密碼。下面是它的代碼。我想放置此代碼,以便通過單擊該菜單可以在應用程序的任何位置調用相同的代碼。有什麼辦法嗎? --- ChangingPassword.java ---Android在整個應用程序中調用相同的警報對話框

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; 

} 

---調用活動--- 新ChangePassword()的ShowDialog(TeacherMain.this,USER_ID,密碼)。 返回true;

+0

創建一個通用類,並通過上下文,並使用方法來顯示AlertDialog – Kunu 2015-04-04 07:42:53

+0

非常感謝@Kunu 我是一個新手,所以u能請詳細說明任何例子或東西.. – abissa 2015-04-04 07:50:38

回答

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

,並從你的活動類調用如下

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

爲您檢查的一部分,你必須刪除嵌套如果不是創造新的AlertDialog聲明

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


} 

使用 alertDialogBu​​ilder.show ();

+0

感謝ü何苦MCH ... 它會幫助我很多。 – abissa 2015-04-04 07:54:07

+0

試試這個,如果你還有任何問題,請隨時詢問。 – Kunu 2015-04-04 07:55:30

+0

請張貼你正在面臨問題的地方。 – Kunu 2015-04-04 09:23:42