gpt4 book ai didi

java - 在 AlertDialog 中显示自定义 View

转载 作者:行者123 更新时间:2023-11-30 03:02:05 28 4
gpt4 key购买 nike

我有一个扩展 LinearLayout 的类。它只包含两个 EditBox。单击某些按钮,我想在警报对话框中加载此类。当我点击按钮时,会显示警告对话框,但不会显示扩展 LinearLayout 的 View 类。代码如下。我被困在这个。我能得到一些解决方案吗??

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

最佳答案

如果你改变它应该工作

alertDialogBuilder.setView(dialog);

作为

alertDialogBuilder.setView(dialog.layout);

关于java - 在 AlertDialog 中显示自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22374827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com