【问题标题】:Custom dialog class does't show the dialog自定义对话框类不显示对话框
【发布时间】:2018-02-26 12:11:10
【问题描述】:

为了使我的应用程序的维护尽可能低,我创建了一个类 Cl_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();
    }
}

该类有时由Fragment 有时由Activity 调用。

这是我使用的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>

这里是我如何称呼它的一个例子:

cl_dialog = new Cl_Dialog(activity.this);
cl_dialog.dialogShowDatePicker();
cl_dialog.show();

关于我在代码中哪里出错的任何线索?

无论如何,我是一个老程序员,我正在尝试整理我一年前写的旧代码,这仍然是在 android 中创建自定义对话框的建议方式吗?

谢谢!

【问题讨论】:

    标签: java android dialog android-alertdialog


    【解决方案1】:

    在setContentView 中,您调用builder.create() 而不为alertDialog 设置结果

    【讨论】:

      猜你喜欢
      • 2012-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 1970-01-01
      相关资源
      最近更新 更多