[英]Custom dialog class does't show the dialog
In order to make my app as much low maintenance as possible I've created a class Cl_dialog, that is supposed to manage the creation of each Dialog of my app: 为了使我的应用程序维护尽可能少,我创建了一个类Cl_dialog,该类应该用于管理应用程序每个Dialog的创建:
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();
}
}
The class is called sometimes by Fragment
sometimes by Activity
. 该类有时被Fragment
调用,有时被Activity
调用。
This is an example of layout.xml
I use: 这是我使用的layout.xml
的示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:datePickerMode="spinner"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<ImageButton
android:layout_width="55dp"
android:layout_height="35dp"
android:src="@drawable/ic_navigation_check"
android:layout_margin="15dp"
android:id="@+id/btn_confirm"
android:background="@color/colorPrimary"/>
</LinearLayout>
And here an example of how I call it: 这里是我如何称呼它的一个例子:
cl_dialog = new Cl_Dialog(activity.this);
cl_dialog.dialogShowDatePicker();
cl_dialog.show();
Any clue about where I am wrong in the code? 关于我在代码中哪里出错的任何线索?
Anyway I am an old programmer and I am trying to arrange old code I wrote year ago, is still this the suggested way to create a custom dialog in android? 无论如何,我是一名老程序员,我正尝试整理一年前编写的老代码,这仍然是在android中创建自定义对话框的建议方法吗?
Thank you! 谢谢!
在setContentView
您可以调用builder.create()
而不设置alertDialog
的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.