2016-07-29 161 views
0

我有以下方法,在允許他訪問Setup菜單之前必須先確認用戶的密碼。爲此,我讓他再次登錄,因爲Parse(我的雲存儲)不允許我確認密碼。到現在爲止還挺好。問題是當我view膨脹到我的AlertDialog使用自定義buttonEditText。你可以在我的下面的代碼中看到我已經評論了Button部分,因爲當我嘗試獲取onClickListener時它不起作用。它什麼都不做。所以我目前使用的是alertDialogPositiveButton,沒關係。第二個問題和真正的問題是我無法檢索到數據插入editTextSUpassword3總是等於空白,而不是null。有人可以幫忙嗎?alertdialog的膨脹佈局不起作用

方法:

public void confirmPassword(){ 

     final LayoutInflater inflateSetup = getLayoutInflater(); 

     AlertDialog.Builder alertSetup = new AlertDialog.Builder(this); 
     alertSetup.setView(inflateSetup.inflate(R.layout.inflate_setup, null)); 
     alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       System.out.println("User has clicked the [confirm] button"); 
       View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null); 
       edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3); 
       SUpassword3 = edtPassword3.getText().toString(); 

       final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser(); 
       ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() { 
        @Override 
        public void done(ParseUser parseUser, ParseException e) { 
         if(e==null){ 
          //password confirmed, logout last user and go to next setup 
          System.out.println("Password [" +SUpassword3+ "] and user [" +beforeConfirmPassword.getUsername()+ "] confirmed, logout last user and go to setup"); 
          beforeConfirmPassword.logOut(); 
         }else{ 
          //passwords don't match 
          System.out.println("Password don't match: [" +SUpassword3 + "] USER [" +beforeConfirmPassword.getUsername()+ "]"); 
         } 
        } 
       }); 
      } 
     }); 
     final AlertDialog dialogSetup = alertSetup.create(); 
     dialogSetup.show(); 


/** 
     btnConfirmPassowod = (Button)viewInflate_setup.findViewById(R.id.btnConfirm); 
     btnConfirmPassowod.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(final View v) { 
       System.out.println("User has clicked the [confirm] button"); 
       final ParseUser beforeConfirmPassword = ParseUser.getCurrentUser(); 
       ParseUser.logInInBackground(beforeConfirmPassword.getUsername(), SUpassword3, new LogInCallback() { 
        @Override 
        public void done(ParseUser parseUser, ParseException e) { 
         if(e==null){ 
          //password confirmed, logout last user and go to next setup 
          System.out.println("Password confirmed, logout last user and go to setup"); 
          beforeConfirmPassword.logOut(); 
         }else{ 
          //passwords don't match 
          dialogSetup.dismiss(); 
          Snackbar.make(v, "Wrong password, try again.", Snackbar.LENGTH_LONG) 
            .setAction("Action", null).show(); 
         } 
        } 
       }); 
      } 
     }); */ 
    } 

inflate_setup.xml(XML文件我膨脹的alertDialog)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/edtPassword3" 
     android:layout_gravity="center_horizontal" 
     android:hint="@string/EN_txt.confirmPassword" 
     /> 
<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="New Button" 
    android:id="@+id/btnConfirm" 
    android:layout_gravity="center_horizontal" /> 

</LinearLayout> 

最終版本(工作):

public void confirmPassword(){ 

     final LayoutInflater inflateSetup = getLayoutInflater(); 
     final View viewInflate_setup = inflateSetup.inflate(R.layout.inflate_setup, null); 

     AlertDialog.Builder alertSetup = new AlertDialog.Builder(this); 
     alertSetup.setView(viewInflate_setup); 
     alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       System.out.println("User has clicked the [confirm] button"); 

       edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3); 
       SUpassword3 = edtPassword3.getText().toString(); 

...

+0

你想從哪裏獲得按鈕? – Shaishav

+0

我編輯了代碼,該按鈕就在xml文件中...我只是忘了複製該部分... –

回答

0

您膨脹了您的onClickListener中的另一個視圖。這就是爲什麼你實際上檢查另一個edtPassword3的文本,而不是用戶可見的文本。它應該是這樣的:

View view = inflateSetup.inflate(R.layout.inflate_setup; 
final EditText edtPassword3 = (EditText)viewInflate_setup.findViewById(R.id.edtPassword3); 
alertSetup.setView(view, null)); 
    alertSetup.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       System.out.println("User has clicked the [confirm] button"); 
       SUpassword3 = edtPassword3.getText().toString(); 
.... 
+0

謝謝!你是對的。 –

0

嘗試使用: Dialog class

// custom dialog 
    final Dialog dialog = new Dialog(context); 
    dialog.setContentView(R.layout.custom); 
    dialog.setTitle("Title..."); 
+0

感謝您的回覆,儘管它對我無效。 –

0

獲取充氣視圖的參考:

View dialogView = inflateSetup.inflate(R.layout.inflate_setup, null); 

在對話框中使用:

alertSetup.setView(dialogView); 

然後訪問按鍵使用:

Button myButton = (Button) dialogView.findViewById(R.id.btnConfirm); 
+0

感謝您的回覆,儘管它對我無效。 –