Android 天气APP(三十一)每日提醒弹窗

文章介绍了如何在Android应用中实现每日弹窗提示,包括设置检查版本更新、弹出对话框的设计、使用Glide加载背景图片、用户自定义设置每日弹窗的开关,以及创建一个应用设置页面进行相关设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android:textStyle=“bold” />

<TextView

android:id=“@+id/tv_temperature”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_week”

android:text=“温度”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_48” />

<TextView

android:id=“@+id/tv_weather_state”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_temperature”

android:text=“天气”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_20”

android:typeface=“monospace” />

<ImageView

android:id=“@+id/iv_weather_state”

android:layout_width=“@dimen/dp_80”

android:layout_height=“@dimen/dp_80”

android:layout_alignParentRight=“true”

android:src=“@mipmap/icon_100” />

<TextView

android:id=“@+id/tv_precipitation”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/iv_weather_state”

android:layout_alignParentRight=“true”

android:layout_marginTop=“@dimen/dp_10”

android:drawableLeft=“@mipmap/icon_weather_prec”

android:drawablePadding=“4dp”

android:text=“降水预告”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_12” />

<TextView

android:id=“@+id/tv_temp_difference”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@+id/tv_weather_state”

android:layout_marginTop=“@dimen/dp_100”

android:text=“温差提示”

android:textColor=“@color/white”

android:textSize=“@dimen/sp_18”

android:typeface=“monospace” />

<com.google.android.material.checkbox.MaterialCheckBox

android:id=“@+id/cb_no_pop_up”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:layout_centerHorizontal=“true”

android:text=“不再弹出”

android:textSize=“@dimen/sp_16”

app:useMaterialThemeColors=“true”

app:buttonTint=“@color/gray”

android:textColor=“@color/gray” />

<View

android:layout_centerHorizontal=“true”

android:layout_below=“@+id/iv_dialog_bg”

android:background=“@color/white”

android:layout_width=“@dimen/dp_1”

android:layout_height=“@dimen/dp_12”/>

<ImageView

android:id=“@+id/iv_close”

android:layout_width=“@dimen/dp_24”

android:layout_height=“@dimen/dp_24”

android:layout_below=“@+id/iv_dialog_bg”

android:layout_centerHorizontal=“true”

android:layout_marginTop=“@dimen/dp_12”

android:src=“@mipmap/icon_close_dialog” />

预览图如下所示(里面的图标没有的话可以去我的源码里面下载,或者自行下载一个,因为是白色的所示我贴了也看不见,CSDN中,不开会员的人无法修改文章的主题颜色,免费的主题,改不了博文的颜色,这一点我觉得很坑,非要你开个会员,吃相太难看了)

在这里插入图片描述

这里面用到了一个style,在app的styles.xml中增加如下代码:

在drawable下新建一个shape_dialog_foreground_bg_12.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>

在app的colors.xml中新增一个颜色

#20000000

布局有了,那么就是先改变背景,再增加数据。

二、每天第一次弹窗


下面进入到MainActivity中,将检查版本更新的方法移动一个位置

在这里插入图片描述

因为自动更新的弹窗也是在每日第一次才弹出,所以公用,不过也要修改一下checkAppVersion里面的逻辑才行。修改后代码如下:

/**

  • 检查APP版本

*/

private void checkAppVersion() {

AppVersion appVersion = LitePal.find(AppVersion.class, 1);//读取第一条数据

Log.d(“appVersion”, new Gson().toJson(appVersion.getVersionShort()));

if (AppStartUpUtils.isTodayFirstStartApp(context)) {//今天第一次打开APP

if (!appVersion.getVersionShort().equals(APKVersionInfoUtils.getVerName(context))) {//提示更新

//更新提示弹窗

showUpdateAppDialog(appVersion.getInstall_url(), appVersion.getChangelog());

}

//设置每日提示弹窗

setTipDialog();

}

}

之前是判断可不可以更新,再判断是否为第一次,现在判断是否为第一次打开。

然后下面的重点就是这个setTipDialog方法了

/**

  • 设置每日弹窗

*/

private void setTipDialog() {

boolean isShow = SPUtils.getBoolean(Constant.EVERYDAY_POP_BOOLEAN, true, context);

if (isShow) {

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

//当所有数据加载完成之后显示弹窗

if (everyDayTipDialog != null) {

return;

}

//弹出每日提醒弹窗

showEveryDayTipDialog();

}

},1000);

}

}

这里用到那么那个系统变量,判断是否可以弹窗这个弹窗,然后延时弹出。

三、弹出每日提示弹窗


/**

  • 每日提示弹窗

*/

private void showEveryDayTipDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(context)

.addDefaultAnimation()//默认弹窗动画

.setCancelable(false)

.setText(R.id.tv_week, DateUtils.getWeekOfDate(new Date()))//星期

.setText(R.id.tv_weather_state, dialogWeatherState)//天气状态

.setText(R.id.tv_precipitation, dialogPrecipitation)//降水预告

.setText(R.id.tv_temp_difference,

WeatherUtil.differenceTempTip(dialogTempHeight, dialogTempLow))//温差提示信息

.setContentView(R.layout.dialog_everyday_tip)//载入布局文件

.setWidthAndHeight(SizeUtils.dp2px(context, 270), ViewGroup.LayoutParams.WRAP_CONTENT)//设置弹窗宽高

.setOnClickListener(R.id.iv_close, v -> {//关闭

everyDayTipDialog.dismiss();

});

everyDayTipDialog = builder.create();

String imgUrl = SPUtils.getString(Constant.EVERYDAY_TIP_IMG, “”, context);

ShapeableImageView bg = everyDayTipDialog.getView(R.id.iv_dialog_bg);

Glide.with(context).load(imgUrl).into(bg);

//温度

Typeface typeface = Typeface.createFromAsset(getAssets(), “fonts/Roboto-Light.ttf”);

TextView temp = everyDayTipDialog.getView(R.id.tv_temperature);

temp.setTypeface(typeface);

temp.setText(dialogTemp + “℃”);

//设置天气状态图标

ImageView weatherStateIcon = everyDayTipDialog.getView(R.id.iv_weather_state);

WeatherUtil.changeIcon(weatherStateIcon, dialogWeatherStateCode);

//不再弹出

MaterialCheckBox cb = everyDayTipDialog.getView(R.id.cb_no_pop_up);

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked) {

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN, false, context);

} else {

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN, true, context);

}

}

});

everyDayTipDialog.show();

}

这里面的代码就是先显示一些要的数据,通过缓存拿到必应的url设置背景,然后在弹窗的底部有一个选中框,选中后再关闭这个弹窗,那么这个弹窗以后都不会再弹出了,除非你再应用设置中进行打开。

到这一步,弹窗就出现了。

四、弹窗的开关


既然是增加用户的体验,那么自然要让用户可以自行控制,于是,我在新增了一个应用设置页面。在ui包下新建一个Empty Activity。命名为SettingActivity。它的xml布局如下:

<?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:orientation=“vertical”

android:fitsSystemWindows=“true”

android:background=“@color/gray_white”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.ui.SettingActivity”>

<androidx.appcompat.widget.Toolbar

android:id=“@+id/toolbar”

android:layout_width=“match_parent”

android:layout_height=“?attr/actionBarSize”

android:background=“@color/white”

app:contentInsetLeft=“@dimen/dp_16”

android:elevation=“@dimen/dp_10”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_scrollFlags=“scroll|enterAlways”

app:navigationIcon=“@mipmap/icon_return”

app:popupTheme=“@style/ThemeOverlay.AppCompat.Light”>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“应用设置”

android:textColor=“@color/black”

android:textSize=“@dimen/sp_18” />

</androidx.appcompat.widget.Toolbar>

<LinearLayout

android:layout_marginTop=“@dimen/dp_12”

android:background=“@color/white”

android:paddingLeft=“@dimen/dp_16”

android:paddingRight=“@dimen/dp_16”

android:paddingTop=“@dimen/dp_8”

android:paddingBottom=“@dimen/dp_8”

android:gravity=“center_vertical”

android:orientation=“horizontal”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<TextView

android:text=“每日弹窗”

android:textColor=“@color/black”

android:textSize=“@dimen/sp_16”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<com.llw.mvplibrary.view.SwitchButton

android:id=“@+id/wb_everyday”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”/>

整体来看这个页面目前只有一个管理每日弹窗的功能,有些孤独,不过后续我可能会加入其他的一些功能,敬请期待。

下面进入SettingActivity。里面的代码如下:

package com.llw.goodweather.ui;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import com.llw.goodweather.R;

import com.llw.goodweather.utils.Constant;

import com.llw.goodweather.utils.SPUtils;

import com.llw.goodweather.utils.StatusBarUtil;

import com.llw.mvplibrary.base.BaseActivity;

import com.llw.mvplibrary.view.SwitchButton;

import org.litepal.util.Const;

import butterknife.BindView;

import butterknife.ButterKnife;

/**

  • 应用设置页面

  • @author llw

*/

public class SettingActivity extends BaseActivity {

@BindView(R.id.toolbar)

Toolbar toolbar;

@BindView(R.id.wb_everyday)

SwitchButton wbEveryday;

@Override

public void initData(Bundle savedInstanceState) {

//白色状态栏

StatusBarUtil.setStatusBarColor(context, R.color.white);

//黑色字体

StatusBarUtil.StatusBarLightMode(context);

Back(toolbar);

boolean isChecked = SPUtils.getBoolean(Constant.EVERYDAY_POP_BOOLEAN,true,context);

wbEveryday.setChecked(isChecked);

wbEveryday.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(SwitchButton view, boolean isChecked) {

if(isChecked){

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN,true,context);

}else {

SPUtils.putBoolean(Constant.EVERYDAY_POP_BOOLEAN,false,context);

}

}

});

}

@Override

public int getLayoutId() {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

面试复习笔记

这份资料我从春招开始,就会将各博客、论坛。网站上等优质的Android开发中高级面试题收集起来,然后全网寻找最优的解答方案。每一道面试题都是百分百的大厂面经真题+最优解答。包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

《960页Android开发笔记》

《1307页Android开发面试宝典》

包含了腾讯、百度、小米、阿里、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。熟悉本文中列出的知识点会大大增加通过前两轮技术面试的几率。

《507页Android开发相关源码解析》

只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。

真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

)**

面试复习笔记

这份资料我从春招开始,就会将各博客、论坛。网站上等优质的Android开发中高级面试题收集起来,然后全网寻找最优的解答方案。每一道面试题都是百分百的大厂面经真题+最优解答。包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

《960页Android开发笔记》

[外链图片转存中…(img-oBJJriEr-1712624206630)]

《1307页Android开发面试宝典》

包含了腾讯、百度、小米、阿里、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。熟悉本文中列出的知识点会大大增加通过前两轮技术面试的几率。

[外链图片转存中…(img-wAzxZ6HD-1712624206630)]

《507页Android开发相关源码解析》

只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。

真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。

[外链图片转存中…(img-7LvFHqjh-1712624206630)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值