AlertDialog.Builder với bố cục tùy chỉnh và EditText; không thể truy cập xem


100

Tôi đang cố gắng tạo một hộp thoại cảnh báo với một EditTextđối tượng. Tôi cần đặt văn bản ban đầu của EditTextchương trình. Đây là những gì tôi có.

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

Tôi cần thay đổi những gì để tôi có thể có một EditTextđối tượng hợp lệ ?

[biên tập]

Vì vậy, người dùng370305 và những người khác đã chỉ ra rằng tôi nên sử dụng alertDialog.findViewById(R.id.label_field);

Thật không may, có một vấn đề khác ở đây. Rõ ràng, việc thiết lập chế độ xem nội dung trên các AlertDialognguyên nhân khiến chương trình gặp sự cố khi chạy. Bạn phải đặt nó trên trình xây dựng.

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

Thật không may, khi bạn làm điều này, alertDialog.findViewById(R.id.label_field);bây giờ trở lại null.

[/biên tập]

Câu trả lời:


236

editTextlà một phần của alertDialogbố cục nên Chỉ cần truy cập editTextvới tham chiếu củaalertDialog

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

Cập nhật:

Bởi vì trong dòng mã dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterNull .

cập nhật mã của bạn như bên dưới và cố gắng hiểu từng dòng mã

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

Cập nhật 2:

Khi bạn đang sử dụng đối tượng View do Inflater tạo để cập nhật các thành phần giao diện người dùng khác, bạn có thể sử dụng trực tiếp setView(int layourResId)phương thức của AlertDialog.Builderlớp, có sẵn từ API 21 trở đi.


22
U cũng có thể làm điều đó như sau:dialogBuilder.setView(R.layout.dialog_layout);
SiavA

4
@SiavA phương pháp này là chỉ có hình thức API 21.
Scaraux

Tôi đã cố gắng hiển thị Dialog và nó không hoạt động trong RecyclerView nhưng cái này đã làm.
Muneeb Mirza,

Bạn có thể sử dụng getLayoutInflater()khi inflaterkhông được xác định.
dấu gạch chéo

1
@saigopi khi bạn ghi đè onClick sẽ có đối số (hộp thoại DialogInterface, int id). Trong phương pháp onClick này, chỉ cần truyền vào hộp thoại.cancel ();
Minkoo

28

Sử dụng cái này

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

nên builder.create().show();, bạn có thể kiểm tra builder.show();mã cho biết thêm chi tiết
Phan Văn Linh

9

Bạn có thể viết:

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

Trong trường hợp bất kỳ ai muốn nó trong Kotlin:

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

Đăng lại @ user370305 của câu trả lời.


2

Thay đổi điều này:

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

đến điều này:

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");
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.