Özel düzen ve EditText ile AlertDialog.Builder; görünüme erişilemiyor


102

Bir EditTextnesneyle bir uyarı iletişim kutusu oluşturmaya çalışıyorum . Başlangıç ​​metnini EditTextprogramlı olarak ayarlamam gerekiyor . İşte sahip olduğum şey.

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

Geçerli bir EditTextnesneye sahip olabilmem için neyi değiştirmem gerekiyor ?

[Düzenle]

Yani, user370305 ve kullanmam gereken diğerleri tarafından belirtildi alertDialog.findViewById(R.id.label_field);

Maalesef burada başka bir sorun var. Görünüşe göre, içerik görünümünü ayarlamak AlertDialogprogramın çalışma zamanında çökmesine neden oluyor. Yapıcıda ayarlamalısın.

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

Ne yazık ki, bunu yaptığınızda alertDialog.findViewById(R.id.label_field);şimdi geri dönüyor null.

[/Düzenle]

Yanıtlar:


238

editTextalertDialogmizanpajın bir parçasıdır, bu nedenle sadece editTextreferansla erişinalertDialog

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

Güncelleme:

Çünkü kod satırında dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterolduğunu Boş .

kodunuzu aşağıdaki gibi güncelleyin ve her bir kod satırını anlamaya çalışın

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

Güncelleme 2:

Diğer UI bileşenlerini güncellemek için Inflater tarafından oluşturulan View nesnesini kullandığınızda , API 21'den itibaren mevcut olan sınıf setView(int layourResId)yöntemini doğrudan kullanabilirsiniz AlertDialog.Builder.


22
U şu şekilde de yapabilir:dialogBuilder.setView(R.layout.dialog_layout);
SiavA

4
@SiavA bu yöntem yalnızca API 21'de kullanılabilir.
Scaraux

Dialog'u görüntülemeye çalışıyordum ve RecyclerView'da çalışmıyordu ama bu yaptı.
Muneeb Mirza

Sen kullanabilirsiniz getLayoutInflater()zaman inflatertanımlı değil.
ters eğik

1
@saigopi onClick'i geçersiz kıldığınızda, bağımsız değişkenler olacaktır (DialogInterface dialog, int id). Bu onClick yönteminde, sadece dialog.cancel () 'i iletin;
Minkoo

29

Bunu kullan

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

olmalı builder.create().show();kontrol edebilirsiniz builder.show();daha fazla ayrıntı için kod
Phan Van Linh

9

Yazabilirsin:

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

3

Herhangi birinin Kotlin'de istemesi durumunda:

val dialogBuilder = AlertDialog.Builder(this)
// ...Irrelevant code for customizing the buttons and title
val dialogView = layoutInflater.inflate(R.layout.alert_label_editor, null)
dialogBuilder.setView(dialogView)

val editText =  dialogView.findViewById(R.id.label_field)
editText.setText("test label")
val alertDialog = dialogBuilder.create()
alertDialog.show()

Reposted user370305 en @ cevap.


2

Bunu değiştir:

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

buna:

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

1
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
/**
 * 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");
}
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.