当前位置: 首页 > news >正文

Android 自定义BaseActivity

直接上代码:

BaseActivity代码:

package com.example.custom.activity;import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.example.custom.tool.TopBar;
/*** 基本Activity* */
public abstract class BaseActivity extends AppCompatActivity{// 导航栏private TopBar topBar;//封装Toast对象private static Toast showToast;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 数据setData(savedInstanceState);// 布局setContentView(setContentLayout());// 控件setControls();}/*** 设置数据* */public abstract void setData(Bundle savedInstanceState);/*** 绑定布局* */public abstract int setContentLayout();/*** 初始化组件* */public abstract void setControls();/*** 顶部导航栏初始化* */public void initTopBar(Activity activity,String title){topBar = new TopBar(activity,title);// 左侧点击事件topBar.setLeftClick(topLeftClick);// 右侧点击事件topBar.setRightClick(topRightClick);}/*** 导航栏左侧按钮点击事件* */View.OnClickListener topLeftClick = new View.OnClickListener() {@Overridepublic void onClick(View view) {showToast("导航栏左侧按钮被点击");}};/*** 导航栏右侧按钮点击事件* */View.OnClickListener topRightClick = new View.OnClickListener() {@Overridepublic void onClick(View view) {showToast("导航栏右侧按钮被点击");}};/*** 显示提示  toast** @param msg 提示信息*/@SuppressLint("ShowToast")public void showToast(String msg) {try {if (null == showToast) {showToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);} else {showToast.setText(msg);}runOnUiThread(new Runnable() {@Overridepublic void run() {showToast.show();}});} catch (Exception e) {e.printStackTrace();//解决在子线程中调用Toast的异常情况处理Looper.prepare();Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();Looper.loop();}}/*** 隐藏软键盘*/public void hideSoftInput() {InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);if (getCurrentFocus() != null && null != imm) {imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);}}/*** 显示软键盘*/public void showSoftInput() {InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);if (getCurrentFocus() != null && null != imm) {imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(), 0);}}/*** 设置屏幕横屏/竖屏* @param  isPortrait (true) 竖屏* @param  isPortrait (false) 横屏*/public void setScreenPortrait(boolean isPortrait) {//设置屏幕是否可旋转if (isPortrait) {// 强制竖屏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);} else {// 强制横屏setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}}}

TopBar代码:

package com.chy.ydy.custom;import android.app.Activity;
import android.content.Context;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;import com.chy.ydy.R;public class TopBar {private Activity activity;private View topView;// topBarprivate LinearLayout tobBarContainer;// tapBar容器private ImageView topLeftImage;// 左侧图标private TextView topTitle;// 标题private ImageView topRightImage;// 右侧图标// searchprivate LinearLayout tobSearchContainer;// searchBar容器private ImageView searchBackImg;// 搜索返回private EditText searchET;// 搜索输入框private TextView searchTV;// 搜索按钮/*** 构造函数* */public TopBar(Activity activity){this.activity = activity;topView = activity.findViewById(R.id.topBarLL);// topBartobBarContainer = topView.findViewById(R.id.tobBarContainer);topLeftImage = topView.findViewById(R.id.topBackImg);// 返回按钮topTitle = topView.findViewById(R.id.topTitle);// 标题topRightImage = topView.findViewById(R.id.topSearchImg);// 搜索// searchBartobSearchContainer = topView.findViewById(R.id.tobSearchContainer);searchBackImg = topView.findViewById(R.id.searchBackImg);searchET = topView.findViewById(R.id.searchET);searchTV = topView.findViewById(R.id.searchTV);}public TopBar(Activity activity,String title){this.activity = activity;topView = activity.findViewById(R.id.topBarLL);// topBartobBarContainer = topView.findViewById(R.id.tobBarContainer);topLeftImage = topView.findViewById(R.id.topBackImg);// 左侧图标topTitle = topView.findViewById(R.id.topTitle);// 标题topTitle.setText(title);// 设置默认值topRightImage = topView.findViewById(R.id.topSearchImg);// 右侧图标// searchBartobSearchContainer = topView.findViewById(R.id.tobSearchContainer);searchBackImg = topView.findViewById(R.id.searchBackImg);searchET = topView.findViewById(R.id.searchET);searchTV = topView.findViewById(R.id.searchTV);}/*** 设置标题* */public TopBar setTitle(String title){if (title.length() > 0){topTitle.setText(title);}return this;}/*** 设置左侧图标* */public TopBar setLeftIco(int resId){topLeftImage.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);topLeftImage.setImageResource(resId);return this;}/*** 左侧图标点击事件* */public TopBar setLeftClick(View.OnClickListener leftClick){if (topLeftImage.getVisibility() == View.VISIBLE){topLeftImage.setOnClickListener(leftClick);}return this;}/*** 设置右侧图标控件隐藏* */public TopBar setRightHidden(){if (topRightImage.getVisibility() == View.VISIBLE){topRightImage.setVisibility(View.INVISIBLE);}return this;}/*** 设置右侧图标* */public TopBar setRightIcon(int resId){topRightImage.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);topRightImage.setImageResource(resId);return this;}/*** 右侧图标点击事件* */public TopBar setRightClick(View.OnClickListener rightClick){if (topRightImage.getVisibility() == View.VISIBLE){topRightImage.setOnClickListener(rightClick);}return this;}/*** 设置左侧搜索图标点击事件* */public TopBar setSearchLeftClick(View.OnClickListener leftSearchClick){if (searchBackImg.getVisibility() == View.VISIBLE){searchBackImg.setOnClickListener(leftSearchClick);}return this;}/*** 设置右侧侧搜索图标点击事件* */public TopBar setSearchRightClick(View.OnClickListener rightSearchClick){if (searchTV.getVisibility() == View.VISIBLE){searchTV.setOnClickListener(rightSearchClick);}return this;}/*** 隐藏顶部栏显示搜索栏* */public TopBar hiddenTopBar(){searchET.setText("");//清空内容// topBar隐藏tobBarContainer.setVisibility(View.GONE);// 搜索栏显示tobSearchContainer.setVisibility(View.VISIBLE);return this;}/*** 显示顶部栏隐藏搜索栏* */public TopBar hiddenSearch(){// topBar隐藏tobBarContainer.setVisibility(View.VISIBLE);// 搜索栏显示tobSearchContainer.setVisibility(View.GONE);// 内容清空searchET.setText("");//收起软键盘InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(searchET.getWindowToken(), 0);return this;}/*** 搜索框;软键盘监听事件* */public TopBar setEditorActionListener (TextView.OnEditorActionListener actionListener){if (searchET.getVisibility() == View.VISIBLE){searchET.setOnEditorActionListener(actionListener);}return this;}/*** 搜索框;内容监听* */public TopBar setTextChangedListener(TextWatcher textWatcher){if (searchET.getVisibility() == View.VISIBLE){searchET.addTextChangedListener(textWatcher);}return this;}}

tap_bar.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="64dp"android:layout_alignParentTop="true"android:background="@color/teal_700"android:id="@+id/topBarLL"android:orientation="horizontal"><!--顶部bar--><LinearLayoutandroid:id="@+id/tobBarContainer"android:layout_width="match_parent"android:layout_height="match_parent"android:visibility="visible"android:orientation="horizontal"><ImageViewandroid:id="@+id/topLeftImg"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:layout_weight="5"android:src="@mipmap/ic_launcher"android:padding="10dp"/><TextViewandroid:id="@+id/topTitle"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="24sp"android:text="标题"android:textColor="@color/white"/><ImageViewandroid:id="@+id/topRightImg"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:layout_weight="5"android:src="@mipmap/ic_launcher"android:padding="10dp"/></LinearLayout></LinearLayout>

使用(MainActivity布局---继承BaseActivity):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><!--引入顶部标题栏--><includelayout="@layout/top_bar"/>**************</RelativeLayout>

相关文章:

Android 自定义BaseActivity

直接上代码&#xff1a; BaseActivity代码&#xff1a; package com.example.custom.activity;import android.annotation.SuppressLint; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.os.Looper; impor…...

基于鲲鹏服务器的LNMP配置

基于鲲鹏服务器的LNMP配置 系统 Centos8 # cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core) 卸载已经存在的旧版本的安装包 # rpm -qa | grep php #查看已经安装的PHP旧版本# rpm -qa | grep php | xargs rpm -e #卸载已经安装的旧版&#xff0c;如果提示有…...

MIT-Missing Semester_Topic 6:Version Control (Git) 练习题

文章目录 练习一练习二练习三练习四练习五练习六练习七 练习一 若还没有 Git 的相关经验&#xff0c;阅读 Pro Git 的前几章或诸如 Learn Git Branching 的相关教程&#xff0c;并在学习的同时从 Git 的数据模型&#xff08;data model&#xff09;的角度思考各 Git 命令。 老师…...

OpenHarmony轻量级内核-LiteOS-M

OpenHarmony轻量级内核 思维导图 https://download.csdn.net/download/lanlingxueyu/88816723 概述 内核是一人操作系统的运算核心,决定着系统的性能和稳定性。它是基于硬件的第一层软件扩充,提供操作系统的基本功能,是操作系统工作的基础。它负责管理系统的进程、内存、…...

TCP 传输控制协议——详细

目录 1 TCP 1.1 TCP 最主要的特点 1.2 TCP 的连接 TCP 连接&#xff0c;IP 地址&#xff0c;套接字 1.3 可靠传输的工作原理 1.3.1 停止等待协议 &#xff08;1&#xff09;无差错情况 &#xff08;2&#xff09;出现差错 &#xff08;3&#xff09;确认丢失和确认迟到…...

ArcGIS学习(六)地理数据库

ArcGIS学习(六)地理数据库 上个任务我们讲了一个非常重要的知识点一一坐标系。这个任务我们带来另外一个很重要的知识点一一地理数据库。 地理数据库的内容相比于坐标系简单很多! 首先,先让我们来学习下地理数据库的理论。 ArcGIS 中的地理数据库(Geodatabase)是一个用…...

保研机试算法训练个人记录笔记(四)——哈希算法

目录 两数之和 字母异位词分组 最长连续序列 力扣热题100——哈希算法 两数之和 给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答…...

ChatGPT实战100例 - (14) 打造AI编程助手 Code Copilot

文章目录 ChatGPT实战100例 - (14) 打造AI编程助手 Code Copilot一、Code Copilot AI编程助手二、制作代码生成器2.1 定义生成器框架2.2 从现有代码提取代码模板三、进行代码优化ChatGPT实战100例 - (14) 打造AI编程助手 Code Copilot 一、Code Copilot AI编程助手 Code Copi…...

表单标记(html)

前言 发现input的type属性还是有挺多的&#xff0c;这里把一些常用的总结一下。 HTML 输入类型 (w3school.com.cn)https://www.w3school.com.cn/html/html_form_input_types.asp text-文本 文本输入,如果文字太长&#xff0c;超出的部分就不会显示。 定义供文本输入的单行…...

Linux文件和目录管理

目录基础 Linux操作系统以目录的方式来组织和管理系统中的所有文件。所谓的目录&#xff0c;就是将所有文件的说明信息采用树状结构组织起来。每个目录节点之下会有文件和子目录。 所有一切都从 ‘根’ 开始&#xff0c;用 ‘/’ 代表, 并且延伸到子目录。 bin&#xff1a;B…...

【go】gorm\xorm\ent事务处理

文章目录 1 gorm1.1 开启事务1.2 执行操作1.3 提交或回滚 2 xorm2.1 开启事务2.2 执行操作2.3 提交或回滚 3 ent3.1 开启事务3.2 执行操作3.3 提交或回滚 前言&#xff1a;本文介绍golang三种orm框架对数据库事务的操作 1 gorm Begin开启事务 tx *gorm.DB 1.1 开启事务 tx :…...

【数据分享】1929-2023年全球站点的逐月平均风速(Shp\Excel\免费获取)

气象数据是在各项研究中都经常使用的数据&#xff0c;气象指标包括气温、风速、降水、能见度等指标&#xff0c;说到气象数据&#xff0c;最详细的气象数据是具体到气象监测站点的数据&#xff01; 有关气象指标的监测站点数据&#xff0c;之前我们分享过1929-2023年全球气象站…...

IP地址详解

IP地址是互联网协议&#xff08;Internet Protocol&#xff09;用于标识并定位网络中主机&#xff08;如计算机、服务器、路由器等&#xff09;的一串数字。它是一个32位的二进制数&#xff0c;通常以四个数字&#xff08;每个数字范围为0-255&#xff09;的形式显示&#xff0…...

Python爬虫http基本原理#2

Python爬虫逆向系列&#xff08;更新中&#xff09;&#xff1a;http://t.csdnimg.cn/5gvI3 HTTP 基本原理 在本节中&#xff0c;我们会详细了解 HTTP 的基本原理&#xff0c;了解在浏览器中敲入 URL 到获取网页内容之间发生了什么。了解了这些内容&#xff0c;有助于我们进一…...

Web Services 服务 是不是过时了?创建 Web Services 服务实例

Web Services 是不是过时了&#xff1f; 今天是兔年最后一天&#xff0c;先给大家拜个早年 。 昨天上午视频面试一家公司需要开发Web Services 服务&#xff0c;这个也没有什么&#xff0c;但还需要用 VB.net 开发。这个是多古老的语言了&#xff0c;让我想起来了 10年 前 写 …...

redis单线程还快的原因

1. 内存存储和高效数据结构&#xff1a; 内存存储&#xff1a; Redis将数据存储在内存中&#xff0c;因此可以实现非常高的读写速度&#xff0c;而无需频繁的磁盘I/O操作。 高效数据结构&#xff1a; Redis内置了丰富且高效的数据结构&#xff0c;如字符串、哈希表、列表、集合…...

【flutter】报错 cmdline-tools component is missing

在flutterSDK目录下&#xff0c;双击flutter_console.bat&#xff0c;调出命令行。 输入flutter doctor&#xff0c;如果第三个诊断为[x]&#xff0c;报cmdline-tools component is missing错&#xff08;我这已经修改好了&#xff0c;所以是勾了&#xff09;&#xff0c;那就可…...

以用户为中心,酷开科技荣获“消费者服务之星”

在企业顺应消费升级的道路中&#xff0c;企业自身不仅要着力强化对于消费者服务意识的提升&#xff0c;并且要树立诚信自律的行业示范带头作用&#xff0c;助力消费环境稳中向好&#xff0c;不断满足人民群众对美好生活的期待。企业的发展需要消费者的认可&#xff0c;酷开科技…...

Days 27 ElfBoard 板 AltiumDesigner 相同电路快速布局布线

在进行设计开发的时候&#xff0c;总会遇到相同的电路&#xff0c;或者模块&#xff0c;这些电路可以使用相同的布局和走线&#xff0c;例如 DC-DC 电源、网口 PHY 电路部分。这类型的电路&#xff0c;我们可以采用AltiumDesigner 中的 Room 进行布局和布线的快速复制&#xff…...

除夕快乐(前端小烟花)

家人们&#xff0c;新的一年好运常在&#xff0c;愿大家在新的一年里得偿所愿&#xff0c;发财暴富&#xff0c;愿大家找到属于自己的那个公主&#xff0c;下面就给大家展示一下给公主的烟花 前端烟花 新的一年&#xff0c;新的挑战&#xff0c;愿我们不忘初心&#xff0c;砥砺…...

挑战杯推荐项目

“人工智能”创意赛 - 智能艺术创作助手&#xff1a;借助大模型技术&#xff0c;开发能根据用户输入的主题、风格等要求&#xff0c;生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用&#xff0c;帮助艺术家和创意爱好者激发创意、提高创作效率。 ​ - 个性化梦境…...

【Python】 -- 趣味代码 - 小恐龙游戏

文章目录 文章目录 00 小恐龙游戏程序设计框架代码结构和功能游戏流程总结01 小恐龙游戏程序设计02 百度网盘地址00 小恐龙游戏程序设计框架 这段代码是一个基于 Pygame 的简易跑酷游戏的完整实现,玩家控制一个角色(龙)躲避障碍物(仙人掌和乌鸦)。以下是代码的详细介绍:…...

Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?

Golang 面试经典题&#xff1a;map 的 key 可以是什么类型&#xff1f;哪些不可以&#xff1f; 在 Golang 的面试中&#xff0c;map 类型的使用是一个常见的考点&#xff0c;其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...

GitHub 趋势日报 (2025年06月08日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 884 cognee 566 dify 414 HumanSystemOptimization 414 omni-tools 321 note-gen …...

Unit 1 深度强化学习简介

Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库&#xff0c;例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体&#xff0c;比如 SnowballFight、Huggy the Do…...

uniapp中使用aixos 报错

问题&#xff1a; 在uniapp中使用aixos&#xff0c;运行后报如下错误&#xff1a; AxiosError: There is no suitable adapter to dispatch the request since : - adapter xhr is not supported by the environment - adapter http is not available in the build 解决方案&…...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

智能AI电话机器人系统的识别能力现状与发展水平

一、引言 随着人工智能技术的飞速发展&#xff0c;AI电话机器人系统已经从简单的自动应答工具演变为具备复杂交互能力的智能助手。这类系统结合了语音识别、自然语言处理、情感计算和机器学习等多项前沿技术&#xff0c;在客户服务、营销推广、信息查询等领域发挥着越来越重要…...

STM32---外部32.768K晶振(LSE)无法起振问题

晶振是否起振主要就检查两个1、晶振与MCU是否兼容&#xff1b;2、晶振的负载电容是否匹配 目录 一、判断晶振与MCU是否兼容 二、判断负载电容是否匹配 1. 晶振负载电容&#xff08;CL&#xff09;与匹配电容&#xff08;CL1、CL2&#xff09;的关系 2. 如何选择 CL1 和 CL…...

【堆垛策略】设计方法

堆垛策略的设计是积木堆叠系统的核心&#xff0c;直接影响堆叠的稳定性、效率和容错能力。以下是分层次的堆垛策略设计方法&#xff0c;涵盖基础规则、优化算法和容错机制&#xff1a; 1. 基础堆垛规则 (1) 物理稳定性优先 重心原则&#xff1a; 大尺寸/重量积木在下&#xf…...