2011-09-30 44 views
0

我有一個在xml文件中定義的視圖。它包含兩個Edittext字段(其他東西如文本)Android - 從alertbuilder對話框中檢索文本輸入

我使用AlertBuilder觸發用戶在兩個edittext字段中輸入文本(如用戶名和密碼)的對話框。當我嘗試檢索字符串並將它們發送給Login()時,兩個字符串都只是空值。到底是怎麼回事?

它似乎不知道字符串數據沒有保存?

這裏是當我展示的對話框中我的應用程序:

SignInDialog.show(ScreenMain.this, 
           "Login", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 

             LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
             View layout = inflater.inflate(R.layout.screen_dialog_login, null); 
             LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(), 
                 ((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString()); 

            } 
           }, 
           "Cancel", 
           new DialogInterface.OnClickListener() { 
            @Override 
            public void onClick(DialogInterface dialog, int which) { 
             dialog.cancel(); 
            } 
           }); 

這裏有一個類我用來實例化一個對話框:

/* login dialog*/ 
static class SignInDialog { 

    public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){ 
     AlertDialog.Builder builder; 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.screen_dialog_login, null); 

     builder = new AlertDialog.Builder(context); 
     builder.setView(layout); 
     if(positive != null && positiveText != null){ 
      builder.setPositiveButton(positiveText, positive); 
     } 
     if(negative != null && negativeText != null){ 
      builder.setNegativeButton(negativeText, negative); 
     } 

     builder.create().show(); 

    } 
} 

回答

1

膨脹一個佈局是創建一個新的實例。 (您沒有收到對現有實例的引用。)因此,在您的onClick中,您正在創建佈局的新副本,並且您的字段不包含任何文本,因爲它們與您的用戶剛輸入的文本不同。

1

爲什麼不完全的子類AlertDialog.Builder,並添加一個方法來檢索EditText值?

0

做這樣的事情:

View layout = inflater.inflate(R.layout.screen_dialog_login, null); 

layout.findViewById(R.id.*yourwidget*); 

我嘗試過了,它幫助

0

這裏是方法我使用:

private void showPopUp3() {   

       AlertDialog.Builder helpBuilder = new AlertDialog.Builder(AlarmReceiverActivity.this); 
       helpBuilder.setTitle("hi"); 
       // helpBuilder.setMessage("This is a Simple Pop Up"); 
       final EditText input = new EditText(this); 
       input.setHeight(20); 
       input.setText(""); 
       LayoutInflater inflater = getLayoutInflater(); 
       final View checkboxLayout = inflater.inflate(R.layout.alarm, null); 


       checkboxLayout.findViewById(R.id.Yes).setOnClickListener(new OnClickListener(){ 
         public void onClick(View arg0) { 
          // setTitle("button2"); 
          checkboxLayout.findViewById(R.id.note).setVisibility(View.VISIBLE); 
         } 
        }); 
       checkboxLayout.findViewById(R.id.No).setOnClickListener(new OnClickListener(){ 
         public void onClick(View arg0) { 
          // setTitle("button2"); 
          checkboxLayout.findViewById(R.id.note).setVisibility(View.INVISIBLE); 
         } 
        }); 
       helpBuilder.setView(checkboxLayout); 

       helpBuilder.setPositiveButton("No", 
        new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
        // Do nothing but close the dialog 
         mMediaPlayer.stop(); 
         finish(); 
        } 
        }); 
       helpBuilder.setNegativeButton("Yes", 
          new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
          // Do nothing but close the dialog 
        mMediaPlayer.stop(); 

         //showSimplePopUp(); 

          } 
          }); 
       // Remember, create doesn't show the dialog 
       AlertDialog helpDialog = helpBuilder.create(); 
       helpDialog.show(); 
      }