自定義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();
    }
}

發佈了32 篇原創文章 · 獲贊 2 · 訪問量 494
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章