Trong Android docs on AlertDialog, nó mang lại cho các hướng dẫn sau đây và ví dụ để thiết lập một giao diện tùy chỉnh trong một AlertDialog:Làm thế nào để thực hiện một tùy chỉnh AlertDialog Xem
If you want to display a more complex view, look up the FrameLayout called "body" and add your view to it:
FrameLayout fl = (FrameLayout) findViewById(R.id.body);
fl.add(myView, new LayoutParams(FILL_PARENT, WRAP_CONTENT));
Trước hết, đó là khá rõ ràng rằng add()
là lỗi đánh máy và có nghĩa là addView()
.
Tôi bị nhầm lẫn bởi dòng đầu tiên sử dụng R.id.body. Có vẻ như đó là phần tử cơ thể của AlertDialog ... nhưng tôi không thể chỉ nhập mã đó vào mã của tôi b/c nó đưa ra một lỗi biên dịch. R.id.body được định nghĩa hoặc phân công ở đâu?
Đây là mã của tôi. Tôi đã cố gắng sử dụng setView(findViewById(R.layout.whatever)
trên trình tạo nhưng nó không hoạt động. Tôi giả định bởi vì tôi đã không tự thổi phồng nó?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title")
.setCancelable(false)
.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText textBox = (EditText) findViewById(R.id.textbox);
doStuff();
}
});
FrameLayout f1 = (FrameLayout)findViewById(R.id.body /*CURRENTLY an ERROR*/);
f1.addView(findViewById(R.layout.dialog_view));
AlertDialog alert = builder.create();
alert.show();
Để tìm và sử dụng đối tượng của bạn trên một Dialog, hãy làm theo bốn bước này: http://stackoverflow.com/a/18773261/1699586 – Sara
Câu trả lời một dòng: thêm '.setView (getLayoutInflater(). Inflate (R.layout.dialog_view, null))' vào trình tạo. Tín dụng cho Sergio Viudes, bên dưới. –