自定义Dialog练习

效果展示:

在这里插入图片描述

主布局代码:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:gravity="bottom"
    android:background="#DDDBDB"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="评一评"
        android:textSize="21sp"
        android:textColor="#FDFDFD"
        android:background="#FF9800"
        android:onClick="onclic"
        />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="首页"
            android:gravity="center"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="发现"
            android:gravity="center"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="搜索"
            android:gravity="center"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="广场"
            android:gravity="center"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:text="我的"
            android:gravity="center"
            />
    </RadioGroup>
</LinearLayout>
自定义布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="180dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:background="#03A9F4"
            android:text="快来评价一下吧!"
            android:textSize="25sp"
            android:gravity="center"
            android:textColor="#ffffff"
            />
        <Button
            android:id="@+id/exit"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:text="退出"
            android:layout_alignParentRight="true"
            android:background="#00000000"
            />
    </RelativeLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#ffffff"
        android:text="喜欢吗?喜欢请给一个五星\n好评,鼓励我们做得更好!"
        android:textSize="18sp"
        android:gravity="center"
        android:textColor="#000000"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#c3c3c3">

    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:id="@+id/bad"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="我要吐槽"
            />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#c3c3c3">

        </View>
        <TextView
            android:id="@+id/good"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="五星好评"
            />
    </LinearLayout>
</LinearLayout>

MyDialog类:
package com.example.homework0927;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        super(context);
    }
    private Button exit;
    private TextView bad,good;
    private GoodClick goodClick;
    private ExitClick exitClick;
    private BadClick badClick;

    @Override
    public String toString() {
        return "MyDialog{" +
                "exit=" + exit +
                ", bad=" + bad +
                ", good=" + good +
                '}';
    }

    public interface GoodClick{
        void onClick();
    }
    public interface BadClick{
        void onClick();
    }
    public interface ExitClick{
        void onClick();
    }

    public void setGoodClick(GoodClick goodClick){
        this.goodClick = goodClick;
    }
    public void setBadClick(BadClick badClick){
        this.badClick = badClick;
    }
    public void setExitClick(ExitClick exitClick){
        this.exitClick = exitClick;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myview);
        exit = (Button) findViewById(R.id.exit);
        bad = (TextView) findViewById(R.id.bad);
        good = (TextView) findViewById(R.id.good);

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                exitClick.onClick();
                dismiss();
            }
        });
        good.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goodClick.onClick();
                dismiss();
            }
        });
        bad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goodClick.onClick();
                dismiss();
            }
        });
    }
}

MainActivity:
package com.example.homework0927;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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



    private void dialog() {
        MyDialog myDialog = new MyDialog(this);
        myDialog.setBadClick(new MyDialog.BadClick() {
            @Override
            public void onClick() {
                Toast.makeText(MainActivity.this, "safsdfsdf", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.setGoodClick(new MyDialog.GoodClick() {
            @Override
            public void onClick() {
                Toast.makeText(MainActivity.this, "safsdfsdf", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.setExitClick(new MyDialog.ExitClick() {
            @Override
            public void onClick() {
                Toast.makeText(MainActivity.this, "safsdfsdf", Toast.LENGTH_SHORT).show();
            }
        });
        myDialog.show();
    }

    public void onclic(View view) {
        dialog();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值