智能管家App kotlin版(2)——工具类封装与首页引导页开发

Tanisha ·
更新时间:2024-09-20
· 937 次阅读

前言:项目最讲究的前期架构搭建,我们把标准的Log和SharedPreferences进行封装,同时开发我们的首页和引导页做一些技巧性的处理,项目开发效率将大大的提高,同时我们继承腾讯的bugly为我们的Carsh做一些约束性的策略!

此篇文章紧做关于该项目的工具类封装与首页引导页开发,后续功能实现请关注后续文章!!!

此篇文章完成后效果展示:
在这里插入图片描述

一.工具类的封装—Log封装

在utils包下,创建L类,代码如下:

package com.zrc.smartbutler.utils import android.util.Log /** *项目名: SmartButler *包名: com.zrc.smartbutler.utils *文件名: L *创建者: 张如成 *创建时间: 2020/5/6 21:21 *描述: Log封装类 */ class L { //开关 val DEBUG = true //TAG val TAG = "Smartbutler" //五个等级 DIWE fun d(text: String?) { if (DEBUG) { Log.d(TAG, text) } } fun i(text: String?) { if (DEBUG) { Log.i(TAG, text) } } fun w(text: String?) { if (DEBUG) { Log.w(TAG, text) } } fun e(text: String?) { if (DEBUG) { Log.e(TAG, text) } } }

这地方没什么好说的,就是对系统自带的Log进行封装,使我们更方便使用!!!
至此,Log封装完成!!!

二.工具类的封装—SharedPreferences封装

在utils包下创建一个类,名为ShareUtils,代码如下:

package com.zrc.smartbutler.utils import android.content.Context import android.content.SharedPreferences /** *项目名: SmartButler *包名: com.zrc.smartbutler.utils *文件名: ShareUtils *创建者: 张如成 *创建时间: 2020/5/6 21:39 *描述: SharedPreferences封装 */ class ShareUtils { val NAME:String = "config" //存储String类型参数 // 键 值 fun putString(mContext:Context,key:String,value:String){ val sp:SharedPreferences = mContext.getSharedPreferences(NAME,Context.MODE_PRIVATE) sp.edit().putString(key,value).commit() } //读取String类型参数 // 键 默认值 fun getString(mContext:Context,key:String,defvalue:String):String{ val sp:SharedPreferences = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) return sp.getString(key, defvalue)!! } //存储Int类型参数 // 键 值 fun putInt(mContext:Context,key:String,value:Int){ val sp:SharedPreferences = mContext.getSharedPreferences(NAME,Context.MODE_PRIVATE) sp.edit().putInt(key,value).commit() } //读取Int类型参数 // 键 默认值 fun getInt(mContext:Context,key:String,defvalue:Int):Int{ val sp:SharedPreferences = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) return sp.getInt(key, defvalue)!! } //存储Boolean类型参数 // 键 值 fun putBoolean(mContext:Context,key:String,value:Boolean){ val sp:SharedPreferences = mContext.getSharedPreferences(NAME,Context.MODE_PRIVATE) sp.edit().putBoolean(key,value).commit() } //读取Boolean类型参数 // 键 默认值 fun getBoolean(mContext:Context,key:String,defvalue:Boolean):Boolean{ val sp:SharedPreferences = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) return sp.getBoolean(key, defvalue)!! } //删除 单个 fun deleShare(mContext:Context,key:String){ val sp:SharedPreferences = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) sp.edit().remove(key).commit() } //删除 全部 fun deleAll(mContext:Context){ val sp:SharedPreferences = mContext.getSharedPreferences(NAME, Context.MODE_PRIVATE) sp.edit().clear().commit() } }

这个封装类,我要做简要说明。

首先,在封装时,定义存储方式 get/put
其次,明确数据类型Int/String/Boolean
最后,定义删除功能 单个/全部

至此,SharedPreferences封装完成!!!

三.首页逻辑—首页跳转逻辑开发

1.创建闪屏页和引导页,名为SplashActivity和GuideActivity。
2.在清单文件中,对闪屏页进行设置,让他第一个启动

就相当于让他和MainActivity换位置,此时MainActivity在清单文件中为:

3.对闪屏页 xml界面进行设置

代码很简单,在此就不赘述!!!
4.对闪屏页进行逻辑开发
在SplashActivity里面进行操作,代码如下:

package com.zrc.smartbutler.ui import android.content.Intent import android.os.Bundle import android.os.Handler import android.os.Message import androidx.appcompat.app.AppCompatActivity import com.zrc.smartbutler.MainActivity import com.zrc.smartbutler.R import com.zrc.smartbutler.utils.ShareUtils import com.zrc.smartbutler.utils.StaticClass import com.zrc.smartbutler.utils.UtilTools import kotlinx.android.synthetic.main.activity_splash.* /** *项目名: SmartButler *包名: com.zrc.smartbutler.ui *文件名: SplashActivity *创建者: 张如成 *创建时间: 2020/5/6 22:05 *描述: 闪屏页 */ class SplashActivity : AppCompatActivity() { /** *1.延时2000ms *2.判断程序是否第一次运行 *3.自定义字体 *4.Activity全屏主题 */ private var handler: Handler? = object : Handler() { override fun handleMessage(msg: Message) { super.handleMessage(msg) when(msg.what){ StaticClass().HANDLER_SPLASH -> { //判断程序是否是第一次运行 if(isFirst()){ startActivity(Intent(this@SplashActivity, GuideActivity::class.java)) }else{ startActivity(Intent(this@SplashActivity, MainActivity::class.java)) } finish() } } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_splash) //初始化View initView() } //初始化View private fun initView() { //延时2秒 handler?.sendEmptyMessageDelayed(StaticClass().HANDLER_SPLASH,2000) //设置字体 UtilTools().setFont(this,tv_splash) } //判断程序是否是第一次运行 private fun isFirst():Boolean{ val isFirst = ShareUtils().getBoolean(this,StaticClass().SHARE_IS_FIRST,true) if(isFirst){ ShareUtils().putBoolean(this,StaticClass().SHARE_IS_FIRST,false) //是第一次运行 return true }else{ //不是第一次运行 return false } } //禁止返回 override fun onBackPressed() { // super.onBackPressed() } }

这段代码主要实现四个功能:
第一个,设置延时2000ms,是通过handler来进行耗时操作

第二个,判断程序是否运行,是使用刚刚封装的ShareUtils进行判断,2秒延时结束后,进行判断,当第一运行时,为true值,进入引导页面,并修改值为false,以后进入,直接进入主页

第三个是自定义字体,在main包下,创建assets/fonts包,并把从网络上下载的字体FONT.TTF放入,在UtilTools里面进行封装,代码如下:

package com.zrc.smartbutler.utils import android.content.Context import android.graphics.Typeface import android.widget.TextView import kotlinx.android.synthetic.main.activity_splash.* /** *项目名: SmartButler *包名: com.zrc.smartbutler.utils *文件名: UtilTools *创建者: 张如成 *创建时间: 2020/5/6 8:07 *描述: 工具统一类 */ class UtilTools { //设置字体 fun setFont(mContext:Context,textView: TextView){ val fontType = Typeface.createFromAsset(mContext.assets,"fonts/FONT.TTF") textView.setTypeface(fontType) } }

并使用:

UtilTools().setFont(this,tv_splash)

第四个是Activity全屏主题,在values/styles.xml文件中,创建样式,代码如下:

true false true @null

然后前往清单文件,对闪屏页进行设置:

android:theme="@style/NoActivityFullscreen"

闪屏页大致就这四部分事情,当然这只是简单描述,想要具体了解,欢迎查看源码,本人注释写的已经很详细了!!!

至此,引导页逻辑开发完成!!!

四.引导页逻辑—引导页逻辑开发

1.首先先编写引导页界面,代码如下:

需要 登录 后方可回复, 如果你还没有账号请 注册新账号