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]
dialogBuilder.setView(R.layout.dialog_layout);