当前位置: 首页 > 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;砥砺…...

盘古信息PCB行业解决方案:以全域场景重构,激活智造新未来

一、破局&#xff1a;PCB行业的时代之问 在数字经济蓬勃发展的浪潮中&#xff0c;PCB&#xff08;印制电路板&#xff09;作为 “电子产品之母”&#xff0c;其重要性愈发凸显。随着 5G、人工智能等新兴技术的加速渗透&#xff0c;PCB行业面临着前所未有的挑战与机遇。产品迭代…...

从WWDC看苹果产品发展的规律

WWDC 是苹果公司一年一度面向全球开发者的盛会&#xff0c;其主题演讲展现了苹果在产品设计、技术路线、用户体验和生态系统构建上的核心理念与演进脉络。我们借助 ChatGPT Deep Research 工具&#xff0c;对过去十年 WWDC 主题演讲内容进行了系统化分析&#xff0c;形成了这份…...

React第五十七节 Router中RouterProvider使用详解及注意事项

前言 在 React Router v6.4 中&#xff0c;RouterProvider 是一个核心组件&#xff0c;用于提供基于数据路由&#xff08;data routers&#xff09;的新型路由方案。 它替代了传统的 <BrowserRouter>&#xff0c;支持更强大的数据加载和操作功能&#xff08;如 loader 和…...

聊聊 Pulsar:Producer 源码解析

一、前言 Apache Pulsar 是一个企业级的开源分布式消息传递平台&#xff0c;以其高性能、可扩展性和存储计算分离架构在消息队列和流处理领域独树一帜。在 Pulsar 的核心架构中&#xff0c;Producer&#xff08;生产者&#xff09; 是连接客户端应用与消息队列的第一步。生产者…...

【SQL学习笔记1】增删改查+多表连接全解析(内附SQL免费在线练习工具)

可以使用Sqliteviz这个网站免费编写sql语句&#xff0c;它能够让用户直接在浏览器内练习SQL的语法&#xff0c;不需要安装任何软件。 链接如下&#xff1a; sqliteviz 注意&#xff1a; 在转写SQL语法时&#xff0c;关键字之间有一个特定的顺序&#xff0c;这个顺序会影响到…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

AGain DB和倍数增益的关系

我在设置一款索尼CMOS芯片时&#xff0c;Again增益0db变化为6DB&#xff0c;画面的变化只有2倍DN的增益&#xff0c;比如10变为20。 这与dB和线性增益的关系以及传感器处理流程有关。以下是具体原因分析&#xff1a; 1. dB与线性增益的换算关系 6dB对应的理论线性增益应为&…...

JavaScript 数据类型详解

JavaScript 数据类型详解 JavaScript 数据类型分为 原始类型&#xff08;Primitive&#xff09; 和 对象类型&#xff08;Object&#xff09; 两大类&#xff0c;共 8 种&#xff08;ES11&#xff09;&#xff1a; 一、原始类型&#xff08;7种&#xff09; 1. undefined 定…...

Webpack性能优化:构建速度与体积优化策略

一、构建速度优化 1、​​升级Webpack和Node.js​​ ​​优化效果​​&#xff1a;Webpack 4比Webpack 3构建时间降低60%-98%。​​原因​​&#xff1a; V8引擎优化&#xff08;for of替代forEach、Map/Set替代Object&#xff09;。默认使用更快的md4哈希算法。AST直接从Loa…...