单选样式的自定义dialog

//dialog的布局

<?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:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/yiJianFanKuiLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#6678A0"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="5dp" >

        <!-- android:background="@drawable/shape_rect" -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="反馈类型" />

        <TextView
            android:id="@+id/checkText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="right"
            android:text="功能意见  〉" />
    </LinearLayout>
    <!-- #6779A0  #D1EEFF -->

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E8EEF7"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:text="请选择反馈类型" />

    <LinearLayout
        android:id="@+id/radioGroup1_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E8EEF7"       
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/gongnengyijian_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <CheckBox
                android:id="@+id/checkBox_gongnengyijian"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:button="@drawable/checkbox_selector_outdata"
                android:paddingLeft="20dp"
                android:text="功能意见" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#B3B3B3" />

        <LinearLayout
            android:id="@+id/yimianyijian_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <CheckBox
                android:id="@+id/checkBox_yimianyijian"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:button="@drawable/checkbox_selector_outdata"
                android:paddingLeft="20dp"
                android:text="页面意见" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#B3B3B3" />

        <LinearLayout
            android:id="@+id/xinxuqiu_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <CheckBox
                android:id="@+id/checkBox_xinxuqiu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:button="@drawable/checkbox_selector_outdata"
                android:paddingLeft="20dp"
                android:text="您的新需求" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#B3B3B3" />

        <LinearLayout
            android:id="@+id/caozuoyijian_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <CheckBox
                android:id="@+id/checkBox_caozuoyijian"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:button="@drawable/checkbox_selector_outdata"
                android:paddingLeft="20dp"
                android:text="操作意见" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#B3B3B3" />

        <LinearLayout
            android:id="@+id/qita_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true" >

            <CheckBox
                android:id="@+id/checkBox_qita"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:button="@drawable/checkbox_selector_outdata"
                android:paddingLeft="20dp"
                android:text="其他" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:background="#B3B3B3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#E8EEF7"        
        android:orientation="vertical"
        android:paddingTop="20dp"
        >

        <Button
            android:id="@+id/btn_yes_dialog"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginLeft="80dp"
            android:layout_marginRight="80dp"
            android:background="@drawable/onlineservicer_selector_button"
            android:gravity="center"
            android:onClick="onClick"
            android:text="确定"
            android:textColor="@drawable/onlineservicer_selector_font"
            android:textSize="12sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="30dp" />
    </LinearLayout>

</LinearLayout>

//dialog java代码片段(包括单选方法)

public static String showTypeDialog(final Context context, final TextView typeTv) {

        // 1.获得dialog
        final Dialog dialog = new Dialog(context, R.style.add_dialog);

        dialog.show();
        dialog.setCanceledOnTouchOutside(true);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

        // 2.获得外框
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        // 3.用自己的布局替换dialog样式
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.feedback_type_dialoglayout, null);
        final TextView checkTextTv = (TextView) view.findViewById(R.id.checkText);

        LinearLayout FunctionLayout = (LinearLayout) view.findViewById(R.id.gongnengyijian_layout);
        LinearLayout UILayout = (LinearLayout) view.findViewById(R.id.yimianyijian_layout);
        LinearLayout NewLayout = (LinearLayout) view.findViewById(R.id.xinxuqiu_layout);
        LinearLayout OptionLayout = (LinearLayout) view.findViewById(R.id.caozuoyijian_layout);
        LinearLayout OtherLayout = (LinearLayout) view.findViewById(R.id.qita_layout);

        final CheckBox FunctionBox = (CheckBox) view.findViewById(R.id.checkBox_gongnengyijian);
        final CheckBox UIBox = (CheckBox) view.findViewById(R.id.checkBox_yimianyijian);
        final CheckBox NewBox = (CheckBox) view.findViewById(R.id.checkBox_xinxuqiu);
        final CheckBox OptionBox = (CheckBox) view.findViewById(R.id.checkBox_caozuoyijian);
        final CheckBox OtherBox = (CheckBox) view.findViewById(R.id.checkBox_qita);
        // 4.获取view中相关组件,添加逻辑

        // RadioGroup rgroup = (RadioGroup)
        // view.findViewById(R.id.radioGroup1_dialog);
        // final RadioButton rbutton = (RadioButton)
        // view.findViewById(rgroup.getCheckedRadioButtonId());

        FunctionLayout.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                FunctionBox.setChecked(true);

            }
        });
        FunctionBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = FunctionBox.getText().toString();
                    checkTextTv.setText(str + " 〉  ");
                    // Log.i("tag", "str=="+str);
                    UIBox.setChecked(false);
                    NewBox.setChecked(false);
                    OptionBox.setChecked(false);
                    OtherBox.setChecked(false);
                }

            }
        });
        UILayout.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                UIBox.setChecked(true);

            }
        });
        UIBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = UIBox.getText().toString();
                    checkTextTv.setText(str + " 〉  ");
                    // Log.i("tag", "str=="+str);
                    FunctionBox.setChecked(false);
                    NewBox.setChecked(false);
                    OptionBox.setChecked(false);
                    OtherBox.setChecked(false);
                }

            }
        });
        NewLayout.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                NewBox.setChecked(true);

            }
        });
        NewBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = NewBox.getText().toString();
                    checkTextTv.setText(str + " 〉  ");
                    // Log.i("tag", "str=="+str);
                    UIBox.setChecked(false);
                    FunctionBox.setChecked(false);
                    OptionBox.setChecked(false);
                    OtherBox.setChecked(false);
                }

            }
        });
        OptionLayout.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                OptionBox.setChecked(true);

            }
        });
        OptionBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = OptionBox.getText().toString();
                    checkTextTv.setText(str + " 〉  ");
                    // Log.i("tag", "str=="+str);
                    UIBox.setChecked(false);
                    NewBox.setChecked(false);
                    FunctionBox.setChecked(false);
                    OtherBox.setChecked(false);
                }

            }
        });
        OtherLayout.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                OtherBox.setChecked(true);

            }
        });
        OtherBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = OtherBox.getText().toString();
                    checkTextTv.setText(str + " 〉  ");
                    // Log.i("tag", "str=="+str);
                    UIBox.setChecked(false);
                    NewBox.setChecked(false);
                    OptionBox.setChecked(false);
                    FunctionBox.setChecked(false);
                }

            }
        });

        Button bYes = (Button) view.findViewById(R.id.btn_yes_dialog);

        /*
         * rgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
         *
         * @Override public void onCheckedChanged(RadioGroup group, int
         * checkedId) { RadioButton rb = (RadioButton)
         * group.findViewById(checkedId); str = (String) rb.getText();
         * checkTextTv.setText(str + "〉"); // Toast.makeText(context,
         * "str="+str, 1).show(); } });
         */
        bYes.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 获取到了用户所选择的内容 str
                if ("功能意见".equals(str)) {
                    typeTv.setText("功能意见" + " 〉 ");
                    type = str;
                } else if ("页面意见".equals(str)) {
                    typeTv.setText("页面意见" + " 〉 ");
                    type = str;
                } else if ("您的新需求".equals(str)) {
                    typeTv.setText("您的新需求" + " 〉 ");
                    type = str;
                } else if ("操作意见".equals(str)) {
                    typeTv.setText("操作意见" + " 〉 ");
                    type = str;
                } else if ("其他".equals(str)) {
                    typeTv.setText("其他" + " 〉 ");
                    type = str;
                }

                dialog.dismiss();
            }
        });

        window.setContentView(view);
        return type;
    }

//activity中的调用方法

public void showDefaultDialog() {
        // 1.获得dialog
        final Dialog dialog = new Dialog(this, R.style.dialogs);

        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        // 点击回退键时,退出当前界面
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    finish();
                    return false;
                } else {
                    finish();
                    return false; // 默认返回 false,这里false不能屏蔽返回键,改成true就可以了
                }
            }
        });
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        // 2.获得外框
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        // 3.用自己的布局替换dialog样式
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.system_default_dialog_layout, null);

        // 4.获取view中相关组件,添加逻辑
        
        Button bYes = (Button) view.findViewById(R.id.btn_yes_dialog);
        Button bCancel = (Button) view.findViewById(R.id.btn_cancel_dialog);

        TouSuLayout = (LinearLayout) view.findViewById(R.id.tousu_layout);
        FaultLayout = (LinearLayout) view.findViewById(R.id.guzhang_layout);
        SongZhiLayout = (LinearLayout) view.findViewById(R.id.songzhi_layout);
        AddPosLayout = (LinearLayout) view.findViewById(R.id.shangwuhezuo_layout);
        OutDataLayout = (LinearLayout) view.findViewById(R.id.yijianfankui_layout);
        ChangeLayout = (LinearLayout) view.findViewById(R.id.BianGeng_layout);
        OtherLayout = (LinearLayout) view.findViewById(R.id.qita_layout);

        TouSuBox = (CheckBox) view.findViewById(R.id.checkBox_tousu);
        FaultBox = (CheckBox) view.findViewById(R.id.checkBox_guzhang);
        SongZhiBox = (CheckBox) view.findViewById(R.id.checkBox_songzhi);
        AddPosBox = (CheckBox) view.findViewById(R.id.checkBox_shangwuhezuo);
        OutDataBox = (CheckBox) view.findViewById(R.id.checkBox_yijianfankui);
        ChangeBox = (CheckBox) view.findViewById(R.id.checkBox_BianGeng);
        OtherBox = (CheckBox) view.findViewById(R.id.checkBox_qita);

        TouSuLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                TouSuBox.setChecked(true);

            }
        });
        TouSuBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = TouSuBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    FaultBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    OtherBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });
        FaultLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                FaultBox.setChecked(true);

            }
        });
        FaultBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = FaultBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    OtherBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });

        SongZhiLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                SongZhiBox.setChecked(true);

            }
        });
        SongZhiBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = SongZhiBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    FaultBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    OtherBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });
        AddPosLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                AddPosBox.setChecked(true);

            }
        });
        AddPosBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = AddPosBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    FaultBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    OtherBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });
        OutDataLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                OutDataBox.setChecked(true);

            }
        });
        OutDataBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = OutDataBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    FaultBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OtherBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });
        OtherLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                OtherBox.setChecked(true);

            }
        });
        OtherBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = OtherBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    FaultBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    ChangeBox.setChecked(false);
                }
            }
        });
        ChangeBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    str = ChangeBox.getText().toString();
                    // Log.i("tag", "str=="+str);
                    TouSuBox.setChecked(false);
                    FaultBox.setChecked(false);
                    SongZhiBox.setChecked(false);
                    AddPosBox.setChecked(false);
                    OutDataBox.setChecked(false);
                    OtherBox.setChecked(false);
                }
            }
        });


        bCancel.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // dialog.cancel();
                finish();

            }
        });
        bYes.setOnClickListener(new android.view.View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 获取到了用户所选择的内容 str
                if ("投诉".equals(str)) {
                //    startActivity(new Intent(context, TouSuActivity.class));
                    Utils.showToast("此功能暂未开通!", context);

                } else if ("故障".equals(str)) {
                    startActivity(new Intent(context, FaultActivity.class));

                } else if ("送纸".equals(str)) {
                    startActivity(new Intent(context, SongZhiActivity.class));

                } else if ("增机/新装机申请".equals(str)) {
                    startActivity(new Intent(context, AddPosOriginalPosActivity.class));

                } else if ("调账".equals(str)) {
                    startActivity(new Intent(context, AccountActivity.class));
                } else if ("变更".equals(str)) {
                    startActivity(new Intent(context, ChangedActivity.class));

                } else if ("其他".equals(str)) {
                    //startActivity(new Intent(context, OtherActivity.class));
                    Utils.showToast("此功能暂未开通!", context);
                } else {
                    Toast.makeText(context, "请选择咨询类型!", 1).show();
                }

            }
        });

        window.setContentView(view);

    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值