Android 方便调用的Dialog工具类

在安卓项目中,对话框是随处可见的,我们可以写一个对话框工具类,调用方便,并且减少代码量。
1.定义布局文件activity_dialog.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/tv_cen"
    android:orientation="vertical"
    >
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textSize="@dimen/text_18"
        android:layout_centerHorizontal="true"
        android:text="提示" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="60dp"
            android:layout_below="@+id/textView"
            android:background="@null"
            android:textSize="@dimen/text_18"
            android:textColor="@color/hintColor_repairing"
            android:text="取消" />
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="60dp"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/button"
            android:background="@null"
            android:textColor="@color/commontitle_bg"
            android:text="确定"
            android:textSize="@dimen/text_18"
            />

    LinearLayout>
LinearLayout>

对话框样式:tv_cen.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />   
    <corners android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"/>
    <stroke android:width="1dp" android:color="@color/common_presscolor" />
shape>

2.具体实现过程:MyDialog.java


public class MyDialog {

    Button button;
    TextView textView;
    Button button2;
    AlertDialog alertDialog;

    private DialogButtonClick mClick;


    public interface DialogButtonClick {
        void cilckComfirmButton(View view);
        void cilckCancleButton(View view);
    }

    public void buttonClickEvent(DialogButtonClick bc){
        if(bc != null){
            mClick = bc;
            cilckEvent();
        }
    }



    public void initDialog(Activity activity,String titleString) {
        alertDialog = new AlertDialog.Builder(activity).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.activity_dialog);

        textView = (TextView)window.findViewById(R.id.textView);
        button = (Button)window.findViewById(R.id.button) ;
        button2 = (Button)window.findViewById(R.id.button2) ;

        textView.setText(titleString);

        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }

    public void cilckEvent(){
        if(button != null){
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    mClick.cilckCancleButton(v);
                }
            });
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    mClick.cilckComfirmButton(v);
                }
            });

        }
    }

}

简单的对话框包括:提示语,取消按钮,确定按钮。提示语可以根据传递的参数改变。按钮通过实现接口,暴露出来两个方法。
3.调用:

 MyDialog myDialog = new MyDialog();
        myDialog.initDialog(this,"提示:是否保存该次测量数据?");
        myDialog.buttonClickEvent(new MyDialog.DialogButtonClick(){
            @Override
            public void cilckComfirmButton(View view ){
        //点击确定按钮
            }
            @Override
            public void cilckCancleButton(View view ){
        //点击取消按钮
            }
        });

作为小菜鸟,每天记录自己的点点滴滴,希望变成大肥菜鸟,哈哈。

你可能感兴趣的:(Android 方便调用的Dialog工具类)