天天看點

自定義Dialog的兩種實作方式

     在android的時常會有視窗會以對話框的形式顯示出來,而自帶的Dialog并不能滿足我們的需求,這時就需要我們自定義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