自定义Dialog

自定义Dialog

在平常开发中就经常会使用dialog来提示用户的操作,今天我在一个项目中我首先自定义了一个BaseDialog,代码如下:

package sms.com.base;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import sms.com.R;

/**
 * Author   : luweicheng on 2017/4/29 0029 12:31
 * E-mail   :[email protected]
 * GitHub   : https://github.com/luweicheng24
 * funcation: Dialog的基类
 */

public abstract class BaseDialog  extends AlertDialog implements View.OnClickListener{
    protected BaseDialog(@NonNull Context context) {
        super(context, R.style.dialogStyle);
        initView();
        initListener();
        initData();

    }

    public abstract void processListener(View view) ;

    public abstract void initListener() ;

    public abstract void initData() ;

    public  abstract void initView();

    @Override
    public void onClick(View v) {
        processListener(v);
    }

    /**
     * 按返回键dialog消失
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }
}

是不是挺正常的,接着定义了一个ConfirmDialog继承BaseDialog,代码如下:

package sms.com.ui.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import sms.com.R;
import sms.com.base.BaseDialog;

/**
 * Author   : luweicheng on 2017/4/29 0029 13:03
 * E-mail   :[email protected]
 * GitHub   : https://github.com/luweicheng24
 * funcation: 确定取消的dialog
 */

public class ConfirmDialog extends BaseDialog {


    private TextView title;
    private TextView cancel;
    private TextView confirm;
    private TextView content;
    private String str_title;
    private String str_content;

    /**
     *  显示Dialog
     * @param context
     * @param title
     * @param content
     * @param listener
     */
    public static void showConfirmDialog(Context context, String title, String content, ConfirmDialog.ConfirmOnClickListener listener) {
        ConfirmDialog dialog = new ConfirmDialog(context);
        dialog.setStr_title(title);
        dialog.setStr_content(content);
        dialog.setConfirmOnClickListener(listener);
        dialog.show();

    }

    protected ConfirmDialog(@NonNull Context context) {
        super(context);
    }


    public void setStr_title(String str_title) {
        this.str_title = str_title;
    }

    public void setStr_content(String str_content) {
        this.str_content = str_content;
    }


    @Override
    public void processListener(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                if (confirmOnClickListener != null) {
                    confirmOnClickListener.onCancel();
                }
                break;
            case R.id.tv_confirm:
                if (confirmOnClickListener != null) {
                    confirmOnClickListener.onConfirm();
                }
        }
        dismiss();

    }

    @Override
    public void initListener() {
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);

    }

    /**
     * 填充数据
     */
    @Override
    public void initData() {
        title.setText(str_title);
        content.setText(str_content);
    }

    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        setContentView(R.layout.alertdialog_layout);
        title = (TextView) findViewById(R.id.tv_title);
        content = (TextView) findViewById(R.id.tv_content);
        cancel = (TextView) findViewById(R.id.tv_cancel);
        confirm = (TextView) findViewById(R.id.tv_confirm);

    }


    /**
     * 确定取消接口回调
     */
    public interface ConfirmOnClickListener {
        void onCancel();

        void onConfirm();
    }

    public void setConfirmOnClickListener(ConfirmOnClickListener confirmOnClickListener) {
        this.confirmOnClickListener = confirmOnClickListener;
    }

    private ConfirmOnClickListener confirmOnClickListener;
}

注释很清楚没任何问题啊,但是一运行就报错:

01-08 10:06:54.670: E/AndroidRuntime(1968): Android.util.AndroidRuntimeException: requestFeature() must be called before adding content
01-08 10:06:54.670: E/AndroidRuntime(1968): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.Java:192)
01-08 10:06:54.670: E/AndroidRuntime(1968): at com.android.internal.app.AlertController.installContent(AlertController.java:234)
01-08 10:06:54.670: E/AndroidRuntime(1968): at android.app.AlertDialog.onCreate(AlertDialog.java:314)
01-08 10:06:54.670: E/AndroidRuntime(1968): at android.app.Dialog.dispatchOnCreate(Dialog.java:335)
01-08 10:06:54.670: E/AndroidRuntime(1968): at android.app.Dialog.show(Dialog.java:248)
01-08 10:06:54.670: E/AndroidRuntime(1968): at com.android.test.showDialog(MainActivity.java:121)
 

百度之后,意思dialog的show方法调用之后才能调用setContentView方法。原来BaseDialog中的那些抽象方法必须在onCreate方法中,查看dialog.show()方法的注释,

Start the dialog and display it on screen. The window is placed in the application layer and opaque. Note that you should not override this method to do initialization when the dialog is shown, instead implement that in onStart()

意思:启动对话框并将其显示在屏幕上。 窗口被放置在应用层中并且是不透明的。 注意,在显示对话框时,不应该重写此方法来进行初始化,而是在onStart();
好了,原来抽象方法应该放在基类的onStart()中:

package sms.com.base;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import sms.com.R;

/**
 * Author   : luweicheng on 2017/4/29 0029 12:31
 * E-mail   :[email protected]
 * GitHub   : https://github.com/luweicheng24
 * funcation: Dialog的基类
 */

public abstract class BaseDialog  extends AlertDialog implements View.OnClickListener{
    protected BaseDialog(@NonNull Context context) {
        super(context, R.style.dialogStyle);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initListener();
        initData();

    }

    public abstract void processListener(View view) ;

    public abstract void initListener() ;

    public abstract void initData() ;

    public  abstract void initView();

    @Override
    public void onClick(View v) {
        processListener(v);
    }

    /**
     * 按返回键dialog消失
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        dismiss();
    }
}

Ok,完美,困扰了我两个小时的问题终于解决;

你可能感兴趣的:(自定义Dialog)