自定义对话框

xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Main3Activity"
    >
    <RelativeLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#FF8222">
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/b"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp" />
        <ImageView
            android:id="@+id/imageHead"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/c"
            android:layout_below="@id/image"
            android:layout_centerHorizontal="true" />
        <LinearLayout
            android:id="@+id/line1"
            android:layout_below="@id/imageHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FF9800">
            <TextView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="总资产"
                android:textSize="25dp"
                android:textColor="#fff"
                android:gravity="center"
                />
            <TextView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="累计收费"
                android:textSize="25dp"
                android:textColor="#fff"
                android:gravity="center"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_below="@id/line1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="#FF9800">
            <TextView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="总资产"
                android:textSize="25dp"
                android:textColor="#fff"
                android:gravity="center"
                />
            <TextView
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="累计收费"
                android:textSize="25dp"
                android:textColor="#fff"
                android:gravity="center"
                />
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </RelativeLayout>
    <RelativeLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
        <Button
            android:id="@+id/buttonOn"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:text="点一点"
            android:textSize="30dp"
            android:background="#FF5722"
            android:textColor="#fff"
            />
    </RelativeLayout>
</LinearLayout>

自定义Dialog

public class MyDialog extends Dialog {
    private Button out;
    private TextView title;
    private TextView message;
    private Button no;
    private Button yes;

    public MyDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_item_dialog2);
        initView();
    }

    //设置标题
    private String mtitle;
    public void setMtitle(String mtitle) {
        this.mtitle = mtitle;
    }

    //设置内容
    private String mMessage;
    public void setmMessage(String mMessage) {
        this.mMessage = mMessage;
    }

    //设置确定按钮
    private String okName;
    public void setOkName(String okName) {
        this.okName = okName;
    }
    private YesOnClick yesOnClick;
    public interface YesOnClick{
        void OnClick();
    }
    public void setYesOnClick(String name,YesOnClick yesOnClick) {
        okName = name;
        this.yesOnClick = yesOnClick;
    }

    //设置取消按钮
    private String noName;
    public void setNoName(String noName) {
        this.noName = noName;
    }
    private NoOnClick noOnClick;
    public interface NoOnClick{
        void OnClick();
    }
    public void setNoOnClick(String name,NoOnClick noOnClick) {
        noName = name;
        this.noOnClick = noOnClick;
    }

    //关闭按钮
    private OutOnClick outOnClick;
    public interface OutOnClick{
        void Onclick();
    }
    public void setOutOnClick(OutOnClick outOnClick) {
        this.outOnClick = outOnClick;
    }

    private void initView() {
        out = (Button) findViewById(R.id.out);
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        //给title赋值
        title = (TextView) findViewById(R.id.title);
        title.setText(mtitle);

        //给message赋值
        message = (TextView) findViewById(R.id.message);
        message.setText(mMessage);

        //给消极按钮赋值,添加点击事件
        no = (Button) findViewById(R.id.no);
        no.setText(noName);
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                noOnClick.OnClick();
                dismiss();
            }
        });

        //给积极按钮赋值,设置点击事件
        yes = (Button) findViewById(R.id.yes);
        yes.setText(okName);
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                yesOnClick.OnClick();
                dismiss();
            }
        });
    }
}

MainActivity

public class Main3Activity extends AppCompatActivity implements View.OnClickListener {

    private Button buttonOn;
    private ImageView imageHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        buttonOn = (Button) findViewById(R.id.buttonOn);
        buttonOn.setOnClickListener(this);
        imageHead = (ImageView) findViewById(R.id.imageHead);
        imageHead.setOnClickListener(this);

        RequestOptions options = new RequestOptions();
        options.circleCrop();
        Glide.with(this).load(R.drawable.c).apply(options).into(imageHead);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonOn:
                MyDialog dialog = new MyDialog(this);
                dialog.setMtitle("评价一下吧!");
                dialog.setmMessage("满意吗?给个五星好评吧!我们会做的更好!");
                dialog.setYesOnClick("五星好评", new MyDialog.YesOnClick() {
                    @Override
                    public void OnClick() {
                        Toast.makeText(Main3Activity.this, "谢谢您的好评,欢迎下次光临!", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.setNoOnClick("我要吐槽", new MyDialog.NoOnClick() {
                    @Override
                    public void OnClick() {
                        Toast.makeText(Main3Activity.this, "对您造成的损失还请谅解,我们一定会努力做到您满意!", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.setOutOnClick(new MyDialog.OutOnClick() {
                    @Override
                    public void Onclick() {
                        Toast.makeText(Main3Activity.this, "不要再逛逛了吗?", Toast.LENGTH_SHORT).show();
                    }
                });
                dialog.show();
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值