对话框的封装

对话框在我们开发app的过程中可以说都会有用到,最简单和最常用的就是AlertDialog对话框了,几行代码就可以把它搞掂,例如:

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(false);// 设置点击dialog外侧不可取消
        builder.setTitle("版本更新提醒");
        builder.setMessage("是否更新版本");//设置文本内容
        builder.setPositiveButton("立刻更新", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 下载最新的版本,显示下载的进度
                downloadNewVersion();
            }
        });
        builder.setNegativeButton("稍后再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();// 在子线程中显示dialog,

这个就是标准的弹出对话框,随时随地都可以使用。但是也会有它的缺陷,可能大家都发现了,它的样式布局是固定的,我们是不可以按需修改的,这个对于我们的用户体验来说就显得差了一点,所以下面请跟着我的思路来和大家封装一个对话框,它可以让你指定xml布局,修改标题,修改按钮文本,按钮颜色和处理事件任务。

public abstract class CommonDialog extends Dialog{
    protected Context context;
    protected View view;
    protected float widthScale=0.84375f;

    public CommonDialog(Context context) {
        super(context, R.style.MyDialog);
        this.context=context;
        init();
    }

    protected void init(){
        view= LayoutInflater.from(context).inflate(initLayoutId(),null);
        ViewUtils.inject(this,view);
        setContentView(view);
        Window window=getWindow();
        WindowManager.LayoutParams params=window.getAttributes();
        //设置对话框在屏幕中间显示
        params.gravity= Gravity.CENTER_VERTICAL;
        window.setAttributes(params);
    }

    public abstract  int initLayoutId();

    @Override
    public void show() {
        if(!isShowing()){
            super.show();
        }
        WindowManager.LayoutParams lp=getWindow().getAttributes();
        lp.width= (int) (UIUtils.getScreenWidth()*widthScale);//设置对话框
        getWindow().setAttributes(lp);
    }

    public View getView(){
        return view;
    }

    //使某个控件消失
    public void goneView(int id){
        if(view!=null){
            View goneView=view.findViewById(id);
            if(goneView!=null){
                goneView.setVisibility(View.INVISIBLE);
            }
        }
    }

    //使某个控件不可见
    public void visibilityView(int id){
        if(view!=null){
            View goneView=view.findViewById(id);
            if(goneView!=null){
                goneView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void switchToActivity(Context context,Class<?> clazz) {
        Intent intent=new Intent(context,clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        dismiss();
    }

    @Override
    public void dismiss() {
        if(view!=null){
            view.clearAnimation();
        }
        if(isShowing()){
           super.dismiss();
        }
    }  
}

下面是对上面对话框的引用,其实这一个才是我想说的通用对话框,它的布局可以是通用的,在具体的场景的时候我们只需要修改它的标题,按钮文本和在回调中处理具体的事件方法。

public class DeleteDialog extends CommonDialog{
    @ViewInject(R.id.btn_cancel)
    private Button btn_cancel;
    @ViewInject(R.id.btn_ok)
    private Button btn_ok;
    @ViewInject(R.id.tv_title)
    private TextView tv_title;

    public DeleteDialog(Context context) {
        super(context);
        setCancelable(false);
    }

    @Override
    public int initLayoutId() {
        return R.layout.dialog_delete;
    }

    public void setContent(String content){
        tv_title.setText(content);
    }
    public void setBtn_cancel(String s){
        btn_cancel.setText(s);
    }
    public void setBtn_ok(String s){
        btn_ok.setText(s);
    }
    public void setOkOnClickListener(View.OnClickListener okOnClickListener){
        btn_ok.setOnClickListener(okOnClickListener);
    }
    public void setCancelOnClickListener(View.OnClickListener cancelOnClickListener){
        btn_cancel.setOnClickListener(cancelOnClickListener);
    }
}

下面是这个对话框的布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:background="@drawable/dialog_shape"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:text="确定删除选中记录吗?"
        android:textColor="#333"
        android:textSize="@dimen/dialog_title_size" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="#AFAFBC" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="@dimen/dialog_button_height"
            android:layout_weight="1"
            android:background="@drawable/dialog_button_single_selector"
            android:text="取消"
            android:textColor="#007AFF"
            android:textSize="@dimen/dialog_ok_size" />

        <View
            android:layout_width="1dp"
            android:layout_height="@dimen/dialog_button_height"
            android:background="#E1E1E1" />

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="0dp"
            android:layout_height="@dimen/dialog_button_height"
            android:layout_weight="1"
            android:background="@drawable/dialog_button_single_selector"
            android:text="确定"
            android:textColor="#007AFF"
            android:textSize="@dimen/dialog_ok_size" />
    </LinearLayout>

</LinearLayout>

@drawable/dialog_button_single_selector的xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true">
         <shape>
             <solid android:color="@color/dialog_option_pressed_bg"/>
             <corners android:bottomLeftRadius="@dimen/dialog_window_ratio"
                 android:bottomRightRadius="@dimen/dialog_window_ratio"/>
         </shape>
     </item>
    <item>
        <color android:color="#0000"/>
    </item>
</selector>

猜你喜欢

转载自blog.csdn.net/Huang_wen_huan/article/details/53214651