ฉันพยายามสร้างกล่องโต้ตอบการแจ้งเตือนกับEditTextวัตถุ ฉันต้องการตั้งค่าข้อความเริ่มต้นของ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วัตถุที่ถูกต้อง
[แก้ไข]
ดังนั้นผู้ใช้ 370305 และคนอื่น ๆ จึงชี้ให้เห็นว่าฉันควรใช้ 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มา
[/ แก้ไข]
dialogBuilder.setView(R.layout.dialog_layout);