AlertDialog.Builder พร้อมเค้าโครงที่กำหนดเองและ EditText; ไม่สามารถเข้าถึงมุมมอง


104

ฉันพยายามสร้างกล่องโต้ตอบการแจ้งเตือนกับ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มา

[/ แก้ไข]

คำตอบ:


240

editTextเป็นส่วนหนึ่งของalertDialogเลย์เอาต์ดังนั้นเพียงแค่เข้าถึงeditTextด้วยการอ้างอิงของalertDialog

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

อัปเดต:

เพราะอยู่ในสายรหัส dialogBuilder.setView(inflater.inflate(R.layout.alert_label_editor, null));

inflaterเป็นNull

อัปเดตโค้ดของคุณดังต่อไปนี้และพยายามทำความเข้าใจโค้ดแต่ละบรรทัด

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

อัปเดต 2:

ในขณะที่คุณใช้ View object ที่สร้างโดย Inflater เพื่ออัปเดตส่วนประกอบ UI อื่น ๆ คุณสามารถใช้setView(int layourResId)method of AlertDialog.Builderclass ได้โดยตรงซึ่งพร้อมใช้งานตั้งแต่ API 21 เป็นต้นไป


23
คุณสามารถทำได้ในฐานะ:dialogBuilder.setView(R.layout.dialog_layout);
SiavA

4
@SiavA วิธีนี้ใช้ได้เฉพาะในรูปแบบ API 21
Scaraux

ฉันพยายามแสดง Dialog แล้ว แต่มันไม่ทำงานใน RecyclerView แต่อันนี้ทำ
Muneeb Mirza

คุณสามารถใช้getLayoutInflater()เมื่อinflaterไม่ได้กำหนดไว้
backslashN

1
@saigopi เมื่อคุณแทนที่ onClick จะมีอาร์กิวเมนต์ (DialogInterface dialog, int id) ในเมธอด onClick นี้ให้ส่งผ่าน dialog.cancel ();
Minkoo

29

ใช้อันนี้

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

คุณควรbuilder.create().show();ตรวจสอบbuilder.show();รหัสเพื่อดูรายละเอียดเพิ่มเติม
Phan Van Linh

9

คุณสามารถเขียน:

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

4

ในกรณีที่มีใครต้องการใน 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()

reposted @ user370305 ของคำตอบ




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");
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.