2014-03-26 64 views
57

我想創建一個帶有EditText對象的警告對話框。我需要以編程方式設置EditText的初始文本。這是我的。AlertDialog.Builder具有自定義佈局和EditText;無法訪問視圖

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
// ...Irrelevant code for customizing the buttons and title 
AlertDialog alertDialog = dialogBuilder.create(); 
LayoutInflater inflater = this.getLayoutInflater(); 
alertDialog.setContentView(inflater.inflate(R.layout.alert_label_editor, null)); 
EditText editText = (EditText) findViewById(R.id.label_field); 
editText.setText("test label"); 
alertDialog.show(); 

我需要更改哪些內容才能擁有有效的EditText對象?

[編輯]

所以,它是由user370305和其他人,我應該使用alertDialog.findViewById(R.id.label_field)指出;

不幸的是,這裏還有另一個問題。顯然,在AlertDialog上設置內容視圖會導致程序在運行時崩潰。你必須在建造者身上設置它。

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
// ...Irrelevant code for customizing the buttons and title 
dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null)); 
AlertDialog alertDialog = dialogBuilder.create(); 
LayoutInflater inflater = this.getLayoutInflater(); 
EditText editText = (EditText) alertDialog.findViewById(R.id.label_field); 
editText.setText("test label"); 
alertDialog.show(); 

不幸的是,當你這樣做的時候,alertDialog.findViewById(R.id.label_field);現在返回null。

[/編輯]

回答

155

editTextalertDialog佈局的一部分,因此僅有與alertDialog

EditText editText = (EditText) alertDialog.findViewById(R.id.label_field); 

更新參考訪問editText:因爲在代碼行

dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterNull

更新您的代碼如下圖所示,並試圖瞭解每行代碼

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
// ...Irrelevant code for customizing the buttons and title 
LayoutInflater inflater = this.getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.alert_label_editor, null); 
dialogBuilder.setView(dialogView); 

EditText editText = (EditText) dialogView.findViewById(R.id.label_field); 
editText.setText("test label"); 
AlertDialog alertDialog = dialogBuilder.create(); 
alertDialog.show(); 
+10

ü也可以做到這一點是: 'dialogBu​​ilder.setView(R.layout.dialog_layout);' – SiavA

+1

@SiavA這種方法是可行的唯一形式API 21. – FrenchFalcon

+0

我一直在試圖顯示對話框,它不是在RecyclerView中工作,但這一個。 –

0
View v=inflater.inflate(R.layout.alert_label_editor, null); 
alertDialog.setContentView(v); 
EditText editText = (EditText)v.findViewById(R.id.label_field); 
editText.setText("test label"); 
alertDialog.show(); 
1

更改此:

EditText editText = (EditText) findViewById(R.id.label_field); 

這樣:

EditText editText = (EditText) v.findViewById(R.id.label_field); 
17

使用此一個

AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
    // Get the layout inflater 
    LayoutInflater inflater = (activity).getLayoutInflater(); 
    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the 
    // dialog layout 
    builder.setTitle(title); 
    builder.setCancelable(false); 
    builder.setIcon(R.drawable.galleryalart); 
    builder.setView(inflater.inflate(R.layout.dialogue, null)) 
    // Add action buttons 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 

        } 
       } 
      }); 
    builder.create(); 
    builder.show(); 
2

你可以寫:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 

// ...Irrelevant code for customizing the buttons and title 

LayoutInflater inflater = this.getLayoutInflater(); 

View dialogView= inflater.inflate(R.layout.alert_label_editor, null);      
dialogBuilder.setView(dialogView); 

Button button = (Button)dialogView.findViewById(R.id.btnName); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     //Commond here...... 

     } 
    }); 

EditText editText = (EditText) 
dialogView.findViewById(R.id.label_field); 

editText.setText("test label"); 

dialogBuilder.create().show(); 
0
/** 
* Shows confirmation dialog about signing in. 
*/ 
private void startAuthDialog() { 
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
    AlertDialog alertDialog = dialogBuilder.create(); 
    alertDialog.show(); 

    alertDialog.getWindow().setLayout(800, 1400); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    View dialogView = inflater.inflate(R.layout.auth_dialog, null); 
    alertDialog.getWindow().setContentView(dialogView); 
    EditText editText = (EditText) dialogView.findViewById(R.id.label_field); 
    editText.setText("test label"); 
}