自定义Dialog的两种实现方式

     在android的时常会有窗口会以对话框的形式显示出来,而自带的Dialog并不能满足我们的需求,这时就需要我们自定义Dialog,我总结了两种实现方式。

    下面我分别用这两种方式实现如下效果:


     一、直接用Dialog类创建

     1.自定义布局

<?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="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="电影叫什么名字"
        android:textSize="17sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#333333" />

    <RadioGroup
        android:id="@+id/rg_group"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="vertical">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="大圣归来" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="爸爸去哪儿" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="奔跑吧兄弟" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="琅琊榜" />
    </RadioGroup>

    <Button
        android:id="@+id/bt_commit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:background="#FEB31C"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:layout_marginBottom="20dp"
        android:text="提交"
        android:textColor="#ffffff"
        android:textSize="15sp" />

</LinearLayout>
       2.将布局加入到Dialog中

dialog = new Dialog(this, R.style.Dialog);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_video_layout, null);
dialog.addContentView(view, new RadioGroup.LayoutParams(400, ViewGroup.LayoutParams.WRAP_CONTENT));
      Dialog的style如下:(Dialog默认是带有标题栏的,在style中将其隐藏)

<style name="Dialog" parent="android:style/Theme.Dialog">
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
</style>
3.給布局文件中的控件设置事件,实现具体需要的逻辑。

radioGroup  = (RadioGroup) view.findViewById(R.id.rg_group);
        dialog.show();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case 1:
                        Toast.makeText(MainActivity.this, "1", 1).show();
                        break;
                    case 2:
                        Toast.makeText(MainActivity.this, "2", 1).show();
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this, "3", 1).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this, "4", 1).show();
                        break;

                }
            }
        });
        Button button = (Button) view.findViewById(R.id.bt_commit);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                dialog.dismiss();
                int i = radioGroup.getCheckedRadioButtonId();
                Toast.makeText(MainActivity.this, "选中的是" + i, 1).show();

            }
        });

二、自定义Dialog,代码如下:

public class CustomDialog extends Dialog {
    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private RadioGroup.OnCheckedChangeListener onCheckedChangeListener;

        public Builder(Context context) {
            this.context = context;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setRadioGroup(RadioGroup.OnCheckedChangeListener onCheckedChangeListener){
            this.onCheckedChangeListener = onCheckedChangeListener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_video_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(400, ViewGroup.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.tv_title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.bt_commit)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.bt_commit)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.bt_commit).setVisibility(View.GONE);
            }
            RadioGroup radioGroup = (RadioGroup) layout.findViewById(R.id.rg_group);
            radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
            dialog.setContentView(layout);
            return dialog;
        }
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    builder = new CustomDialog.Builder(this);
    builder.setTitle("电影叫什么名字");
    builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            int i = radioGroup.getCheckedRadioButtonId();
            Toast.makeText(MainActivity.this, "选中的是" + i, 1).show();
        }

    });
    builder.setRadioGroup(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
                case 1:
                    Toast.makeText(MainActivity.this, "1", 1).show();
                    break;
                case 2:
                    Toast.makeText(MainActivity.this, "2", 1).show();
                    break;
                case 3:
                    Toast.makeText(MainActivity.this, "3", 1).show();
                    break;
                case 4:
                    Toast.makeText(MainActivity.this, "4", 1).show();
                    break;
            }
        }
    });
    builder.create().show();
}



本文参考:http://blog.csdn.net/duanyanrui/article/details/8494767 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值