android: 用xpopup做弹窗

一,安装第三方库

库地址:

https://mvnrepository.com/artifact/com.github.li-xiaojun/XPopup

编辑build.gradle,添加:

    // https://mvnrepository.com/artifact/com.github.li-xiaojun/XPopup
    implementation 'com.github.li-xiaojun:XPopup:2.10.0'

然后点击Sync Now

二,代码

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="wrap_content"
    android:orientation="vertical"
    android:layout_marginBottom="100dp"
    android:background="@drawable/rounded_corner"
    android:paddingBottom="100dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Custom Popup"
android:layout_gravity="center"
android:textSize="18sp"
android:textStyle="bold"/>

<Button
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
android:text="关闭"/>
</LinearLayout>

 java:弹出窗口

package com.example.okdemo1.widget;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.okdemo1.R;
import com.example.okdemo1.activity.SmarttabActivity;
import com.lxj.xpopup.core.BottomPopupView;

// 自定义弹窗类
// 自定义弹窗类
public class BottomPopup extends BottomPopupView {

    private String content;
    private TextView contView;
    private Context context;
    public BottomPopup(@NonNull Context context,String content) {
        super(context);
        this.context = context;
        this.content = content;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.bottom_popup; // 指定自定义布局
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        contView = findViewById(R.id.title);
        contView.setText(this.content);
        // 设置按钮点击事件
        findViewById(R.id.close_button).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "你点击了按钮,弹窗将关闭",
                        Toast.LENGTH_SHORT).show();
                 // 点击事件处理逻辑
                dismiss();
            }
        });
    }
}

java: activity

package com.example.okdemo1.activity;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.okdemo1.R;
import com.example.okdemo1.widget.BottomPopup;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.ConfirmPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;

public class SmarttabActivity extends AppCompatActivity {

    private ConfirmPopupView popupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_smarttab);

        //给按钮增加点击事件:不带视图的弹窗
        Button button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                msgPop("通知","请小区居民到广场参加帮助贫困山区义卖活动");
            }
        });

        //给按钮增加点击事件:带视图的弹窗
        Button button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String bottomMsg = "电影哪吒二票房已达158亿";
                // 使用弹窗
                new XPopup.Builder(SmarttabActivity.this)
                        .hasBlurBg(true)
                        .animationDuration(1000)
                        .asCustom(new BottomPopup(SmarttabActivity.this,bottomMsg))
                        //.animation
                        .show();
            }
        });
    }

    //无需视图的代码创建弹窗
    private void msgPop(String title,String content) {
        popupView = new XPopup
                .Builder(this)
                .hasBlurBg(true)
                .animationDuration(1000)
                .asConfirm(title,
                        content,
                        "取消",
                        "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                Toast.makeText(SmarttabActivity.this, "你点击了确定按钮",
                                        Toast.LENGTH_SHORT).show();
                            }
                        },
                        new OnCancelListener() {
                            @Override
                            public void onCancel() {
                                Toast.makeText(SmarttabActivity.this, "你点击了取消按钮",
                                        Toast.LENGTH_SHORT).show();
                            }
                        },
                        false);//单按钮
        popupView.show();

    }
}

三,测试效果

 

posted @ 2025-06-07 11:18  刘宏缔的架构森林  阅读(312)  评论(0)    收藏  举报