Android 自定义布局Dialog,解决版本不兼容问题

日常开发中我们可能会需要自己定义布局的Dialog,这里就记录一下如何使用。
1.新建SignatureDialogFragment类继承与DialogFragment
2.重写onCreateDialog方法
3.新建布局文件dialog_signature

4.在onCreateDialog获取dialog_signature布局实例

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.dialog_signature, null);
       // 获取具体的控件实例
        mSignaturePad = view.findViewById(R.id.signature_pad);
      //确定按钮
        builder.setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callback != null) {
//回掉接口
                    callback.onClick(mSignaturePad.getSignatureBitmap());
                }
            }
        });
//取消按钮
        builder.setView(view).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {


            }
        });

        return builder.create();
    }

5.定义回掉接口

    public interface Callback {
        void onClick(Bitmap bitmap);
    }

    private Callback callback;

6.重写onAttach方法

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Callback) {
            callback = (Callback) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement Callback");
        }
    }

7.在Android版本较低时没有onAttach(Context context)方法所以需要onAttach(Activity activity)方法替代

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            onCurrentAttach(activity);
        }
    }

    /**
     * 重写此方法替代onAttach()高低版本不兼容
     * @param context
     */
    protected void onCurrentAttach(Context context){
        if (context instanceof Callback) {
            callback = (Callback) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement Callback");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        callback = null;
    }

8.Dialog的显示方法:

SignatureDialogFragment viewDialogFragment = new SignatureDialogFragment();
 viewDialogFragment.show(getFragmentManager());

全部代码如下:

public class SignatureDialogFragment extends DialogFragment {

    public interface Callback {
        void onClick(Bitmap bitmap);
    }

    private Callback callback;

    private SignaturePad mSignaturePad;

    public void show(FragmentManager fragmentManager) {
        show(fragmentManager, "SignatureDialogFragment");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        final View view = inflater.inflate(R.layout.dialog_signature, null);
        mSignaturePad = view.findViewById(R.id.signature_pad);

        builder.setView(view).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (callback != null) {
//                            EditText et_userName = (EditText) view.findViewById(R.id.username);
//                            EditText et_password = (EditText) view.findViewById(R.id.password);
                    callback.onClick(mSignaturePad.getSignatureBitmap());
                }
            }
        });
        builder.setView(view).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {


            }
        });

        return builder.create();
    }

//    @Override
//    public void onStart() {
//        super.onStart();
//        DisplayMetrics dm = new DisplayMetrics();
//        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
//    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            onCurrentAttach(activity);
        }
    }

    /**
     * 重写此方法替代onAttach()高低版本不兼容
     * @param context
     */
    protected void onCurrentAttach(Context context){
        if (context instanceof Callback) {
            callback = (Callback) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement Callback");
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof Callback) {
            callback = (Callback) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement Callback");
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        callback = null;
    }

}

注:SignaturePad是一个手写板控件

你可能感兴趣的:(android)