2012-06-02 49 views
0

我的場景,主屏幕上有一個按鈕,當用戶單擊該按鈕時,會出現一個對話框,其中包含一個textedit和2按鈕。我的問題是,當我嘗試從編輯文本中獲取值時,它似乎什麼都沒有發生,並且該值始終爲NULL。如何從customDialog獲取值EditText

這是我的代碼:我宣佈的主要活動

private void popup() { 
     AlertDialog.Builder builder; 
     AlertDialog alertDialog; 

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
     View dialog = inflater.inflate(R.layout.isbn_dialog, 
             (ViewGroup) findViewById(R.id.layout_root)); 

        // The edit text from dialog 
     isbnInput = (EditText)dialog.findViewById(R.id.isbn); 

     builder = new AlertDialog.Builder(this); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     builder.setPositiveButton("Add", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG); 
      } 
     }); 

     builder.setView(dialog); 
     alertDialog = builder.create(); 
     alertDialog.setTitle("Enter ISBN Number"); 
     alertDialog.show(); 
} 

所以,我怎樣才能從對話框編輯文本正確值對話框裏面?

回答

1

該行必須,

Toast.makeText(getApplicationContext(), isbnInput.getText().toString(), Toast.LENGTH_LONG).show(); 

您忘記show()吐司按鈕的點擊裏面..

試試吧..

0

我因此未看到AlertDialog你editext參考。

試試這個,和你一樣,而且肯定的是,我能得到正確editText

AlertDialog.Builder alert = new AlertDialog.Builder(this); 
    alert.setMessage(getResources().getString(R.string.my_email)); 
    final EditText input = new EditText(this); 



    final LinearLayout layout = new LinearLayout(this); 
    LayoutParams params = new LayoutParams(
      (int) getResources().getDimension(R.dimen.simple_alert_width_normal), 
      LayoutParams.WRAP_CONTENT); 
    params.setMargins(10, 0, 10, 0); 
    layout.addView(input, params); 

    alert.setView(layout); 
    alert.setPositiveButton(R.string.OK, 
      new DialogInterface.OnClickListener() { 
      // DO some thing here 

      }); 

    alert.setNegativeButton(R.string.cancel, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 

       } 
      }); 

    alert.show();