java自定义对话框类不显示该对话框
为了使我的应用程序尽可能低维护,我创建了一个class Cl_对话框,用于管理我的应用程序的每个对话框的创建:
public class Cl_Dialog
{
private Activity activity;
private AlertDialog alertDialog;
private AlertDialog.Builder builder;
private View layout;
public Cl_Dialog( Activity act )
{
this.activity = act;
builder = new AlertDialog.Builder( act );
alertDialog = builder.create();
}
public void dialogShowDatePicker()
{
setContentView( R.layout.dialog_datepicker);
( (ImageButton) layout.findViewById( R.id.btn_confirm ) ).setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick( View view )
{
close();
}
});
}
private void setContentView( int idLayout)
{
LayoutInflater inflater = (LayoutInflater) activity.getSystemService( activity.LAYOUT_INFLATER_SERVICE );
layout = inflater.inflate( idLayout, (ViewGroup ) activity.findViewById( R.id.mainLayout ) );
builder.setView( layout );
builder.create();
}
public void show()
{
alertDialog.show();
}
public void close()
{
alertDialog.dismiss();
}
}
该类有时由Fragment
调用,有时由Activity
调用
这是我使用的layout.xml
示例:
<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/mainLayout">
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content">
<DatePicker
安卓:id="@+id/datePicker"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:datePickerMode="spinner"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:gravity="center">
<ImageButton
安卓:layout_width="55dp"
安卓:layout_height="35dp"
安卓:src="@drawable/ic_navigation_check"
安卓:layout_margin="15dp"
安卓:id="@+id/btn_confirm"
安卓:background="@color/colorPrimary"/>
</LinearLayout>
下面是我如何称呼它的一个例子:
cl_dialog = new Cl_Dialog(activity.this);
cl_dialog.dialogShowDatePicker();
cl_dialog.show();
关于我在代码中的错误有什么线索吗
无论如何,我是一个老程序员,我正在尝试整理我一年前写的旧代码,这仍然是在安卓中创建自定义对话框的建议方法吗
谢谢大家!
# 1 楼答案
在
setContentView
中调用builder.create()
,而不设置alertDialog
的结果