Android实现优惠券弹出窗口界面

任务要求:
需要提交所有类的java源文件代码和布局文件代码(xml文件),注意:提交java源文件代码时选择代码语言为Java,提交布局文件代码时选择代码语言为XML

任务描述:
选择合适方式实现如下程序的弹出窗口界面,程序主界面不做要求,需实现当程序启动以后能够弹出下图所示的窗口,并且点击窗口右上角的x号时能够关闭该窗口。界面中所需图片下载地址链接:https://pan.baidu.com/s/1fnh7z9Smqsx8f-_aCjBOcw
提取码:zpoh
在这里插入图片描述

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/btn_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="弹出优惠券窗口"/>
</LinearLayout>

coupons_layout.xml:

<?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"
    android:gravity="center">
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="160dp"
            android:background="@android:color/holo_red_dark">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:background="@drawable/bg"
                android:layout_centerVertical="true"/>
            <Button
                android:id="@+id/btn_close"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/close"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_marginTop="2dp"
                android:layout_marginRight="2dp"/>
        </RelativeLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:paddingTop="10dp"
            android:paddingBottom="10dp">

            <ImageView
                android:id="@+id/iv_item"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:layout_marginLeft="5dp"
                android:background="@drawable/item" />
            <LinearLayout
                android:layout_toRightOf="@id/iv_item"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="幸福西饼满减券"
                        android:textSize="18sp"
                        android:layout_marginTop="4dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="¥20"
                        android:textColor="@android:color/holo_red_dark"
                        android:textSize="23sp"
                        android:layout_marginLeft="5dp"/>
                </LinearLayout>
                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <LinearLayout
                        android:id="@+id/linear_center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="6dp"
                            android:text="满59元可用"
                            android:textSize="16sp" />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="领取后当日有效"
                            android:textSize="16sp" />
                    </LinearLayout>
                    <Button
                        android:layout_toRightOf="@id/linear_center"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/button"
                        android:layout_gravity="center"
                        android:text="去使用"
                        android:textSize="16sp"
                        android:textColor="@android:color/holo_red_dark"/>
                </RelativeLayout>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

strings.xml:

<resources>
    <string name="app_name">customdialog</string>
</resources>

MainActivity.java:

package com.example.homework04;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MainActivity extends AppCompatActivity {

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

        Button btnPopup = findViewById(R.id.btn_popup);
        btnPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
    }

    public void showPopupWindow(){
        //创建PopupWindow对象
        final PopupWindow popupWindow = new PopupWindow(this);
        //设置弹出窗口的宽度和高度
        popupWindow.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        //设置它的视图
        View view = getLayoutInflater().inflate(R.layout.coupons_layout,null);
        //设置视图当中控件的属性和监听器
        Button btnClose = view.findViewById(R.id.btn_close);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭弹出窗
                popupWindow.dismiss();
            }
        });
        popupWindow.setContentView(view);
        //显示PopupWindow
        LinearLayout root = findViewById(R.id.root);
        popupWindow.showAtLocation(root, Gravity.CENTER,0,0);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以通过以下步骤来实现前端文件上传成功后刷新窗并刷新页面的效果: 1. 在 HTML 文件中,创建一个用于文件上传的表单元素: ```html <form id="uploadForm" enctype="multipart/form-data"> <input type="file" name="file" id="fileInput"> <input type="submit" value="Upload"> </form> ``` 2. 使用 JavaScript 监听表单的提交事件,并在提交时执行相应的操作: ```javascript document.getElementById('uploadForm').addEventListener('submit', function(e) { e.preventDefault(); // 阻止默认的表单提交行为 var fileInput = document.getElementById('fileInput'); var file = fileInput.files[0]; if (file) { var formData = new FormData(); formData.append('file', file); // 使用 AJAX 发送文件上传请求 var xhr = new XMLHttpRequest(); xhr.open('POST', '/upload', true); xhr.onload = function() { if (xhr.status === 200) { // 文件上传成功后执行以下操作 alert('文件上传成功'); location.reload(); // 刷新当前页面 } else { alert('文件上传失败'); } }; xhr.send(formData); } }); ``` 在上面的代码中,我们首先获取文件输入框的内容,然后创建一个 FormData 对象,将文件添加到其中。接下来,我们使用 AJAX 发送文件上传请求,并在请求成功时一个成功提示框,然后通过 `location.reload()` 方法刷新当前页面。 请注意,上述示例中的 `/upload` 是一个示意的服务器端接口,你需要根据你的实际情况进行替换。此外,你可能还需要根据你的项目需求对代码进行适当的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FF小迷糊吖~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值