自定义AlertDialog

dialog_exit_order.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="match_parent"
    android:background="@drawable/shape_transform"
    android:orientation="vertical"
    android:paddingTop="16dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="回撤原因"
        android:textColor="#000"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="#000" />

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:orientation="vertical"
        android:paddingLeft="11dp"
        android:paddingRight="11dp" >

        <RadioButton
            android:id="@+id/rb0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:checked="true"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />

        <RadioButton
            android:id="@+id/rb6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:button="@null"
            android:drawableRight="@drawable/radiobutton_selector"
            android:paddingLeft="6dp"
            android:text=" "
            android:textColor="@drawable/color_redio_button"
            android:textSize="16sp"
            android:visibility="gone" />
    </RadioGroup>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:paddingLeft="11dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="备注:"
            android:textColor="#000"
            android:textSize="16sp" />

        <RelativeLayout
            android:layout_width="220dp"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/et_input"
                android:layout_width="180dp"
                android:layout_height="wrap_content"
                android:background="#0000"
                android:cursorVisible="false"
                android:hint=" "
                android:textColor="#000"
                android:textSize="13sp" />

            <View
                android:layout_width="180dp"
                android:layout_height="0.5dp"
                android:layout_below="@id/et_input"
                android:background="#000" />
        </RelativeLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="16dp"
        android:background="#000" />

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

        <TextView
            android:id="@+id/ok"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="确定"
            android:textColor="#2196f3"
            android:textSize="16sp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#000" />

        <TextView
            android:id="@+id/cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="取消"
            android:textColor="#2196f3"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

ExitOrderDialog

package com.clt.runman.widget;

import java.util.List;

import android.R.integer;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.clt.runman.R;

public class ExitOrderDialog extends AlertDialog {

        private Context ctx;
        private View customView;
        private List<String> info;
        private EditText eText;
        private RadioGroup rg;
        private RadioButton rb0;
        private RadioButton rb1;
        private RadioButton rb2;
        private RadioButton rb3;
        private RadioButton rb4;
        private RadioButton rb5;
        private RadioButton rb6;

        private int checkedId = R.id.rb0;
        private String message = "";

        public ExitOrderDialog(Context context, List<String> info) {
            super(context);
            this.ctx = context;
            this.info = info;
            LayoutInflater inflater = LayoutInflater.from(context);
            customView = inflater.inflate(R.layout.dialog_exit_order, null);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            this.setContentView(customView);

            eText = (EditText) customView.findViewById(R.id.et_input);
            rg=(RadioGroup)findViewById(R.id.rg);
            rb0=(RadioButton)findViewById(R.id.rb0);
            rb1=(RadioButton)findViewById(R.id.rb1);
            rb2=(RadioButton)findViewById(R.id.rb2);
            rb3=(RadioButton)findViewById(R.id.rb3);
            rb4=(RadioButton)findViewById(R.id.rb4);
            rb5=(RadioButton)findViewById(R.id.rb5);
            rb6=(RadioButton)findViewById(R.id.rb6);

            initRadioGroup();

            TextView cancel = (TextView) customView.findViewById(R.id.cancel);
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 隐藏当前对话框
                    dismiss();
                }
            });

            TextView ok = (TextView) customView.findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 待做任务 提交相应的操作
                switch (checkedId) {
                case R.id.rb0:
                    message = info.get(0);
                    break;
                case R.id.rb1:
                    message = info.get(1);
                    break;
                case R.id.rb2:
                    message = info.get(2);
                    break;
                case R.id.rb3:
                    message = info.get(3);
                    break;
                case R.id.rb4:
                    message = info.get(4);
                    break;
                case R.id.rb5:
                    message = info.get(5);
                    break;
                case R.id.rb6:
                    message = info.get(6);
                    break;
                }
                Toast.makeText(ctx, "信息:" + message + inputString, Toast.LENGTH_LONG).show();
                // 隐藏当前对话框
                dismiss();
            }
        });


        }

        String inputString = "";
        /**
         * 初始化RadioG
         *@Description: (这里用一句话描述这个方法的作用) 
         *@Author: 郭永振
         *@Since: 2016-12-16下午5:15:26
         */
        private void initRadioGroup() {

            // 初始化radioButon
            for (int i = 0; i < info.size(); i++) {
                RadioButton rb = (RadioButton) rg.getChildAt(i);
                rb.setVisibility(View.VISIBLE);
                rb.setText(info.get(i));
            }

            rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId1) {
                    checkedId = checkedId1;
                }
            });

            eText.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    eText.setCursorVisible(true);

                }
            });

            eText.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    inputString = s.toString();
                }
            });
        }

        @Override
        public View findViewById(int id) {
            return super.findViewById(id);
        }

        public View getCustomView() {
            return customView;
        }
    }

shape_transform.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="3dp"
        android:bottomRightRadius="3dp"
        android:topLeftRadius="3dp"
        android:topRightRadius="3dp" >
    </corners>

    <solid android:color="#fefefe" >
    </solid>

</shape>

radiobutton_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/checked_1" android:state_checked="true"/>
    <item android:drawable="@drawable/unchecked_1" android:state_checked="false"/>

</selector>

展示弹出框的方法

public void exitOrder(View view) {

        List<String> messages = new ArrayList<String>();

        messages.add("1.临时有事,预约到其他时间");
        messages.add("2.操作错误");
        messages.add("3.其他");
        // 弹出警告提示框 提示需要选择的回撤订单的原因
//      Toast.makeText(this, "点击了订单回撤按钮", Toast.LENGTH_LONG).show();
        ExitOrderDialog dialog = new ExitOrderDialog(this, messages);

        dialog.show();

        // 将对话框的大小按屏幕大小的百分比设置
        WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int)(display.getWidth() * 0.8); //设置宽度
        dialog.getWindow().setAttributes(lp);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值