【发布时间】:2014-03-13 18:42:19
【问题描述】:
我有一个扩展 LinearLayout 的类。它只包含两个EditBox。单击某些按钮时,我想在警报对话框中加载此类。当我单击按钮时,会显示警报对话框,但不会显示扩展 LinearLayout 的视图类。代码如下。我陷入了困境。我能得到一些解决方案吗??
public class StudentDialog extends LinearLayout {
Context context;
LinearLayout layout;
public StudentDialog(Context context) {
super(context);
this.context = context;
createStudentDialog();
}
private void createStudentDialog() {
layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.setPadding(10, 10, 10, 10);
layout.setId(200);
EditText studentName = new EditText(context);
studentName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
studentName
.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
EditText address = new EditText(context);
address.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
address.setTextAppearance(getContext(), TEXT_DIRECTION_FIRST_STRONG);
layout.addView(studentName);
layout.addView(address);
}
}
//Now i am calling this on some button click listener as follows. The alert dialog is displayed but not the StudentDialog.
StudentDialog dialog = new StudentDialog(this);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialog);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
【问题讨论】:
-
请关注stackoverflow.com/a/18224754/942224,在此您扩展了 Dialog 而不是 View,只需创建该类的对象,您就可以使用 show 作为对话框查看。
标签: java android android-alertdialog android-custom-view