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

深入剖析AI大模型:大模型时代的 Prompt 工程全解析

今天聊的内容&#xff0c;我认为是AI开发里面非常重要的内容。它在AI开发里无处不在&#xff0c;当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗"&#xff0c;或者让翻译模型 "将这段合同翻译成商务日语" 时&#xff0c;输入的这句话就是 Prompt。…...

【Linux】C语言执行shell指令

在C语言中执行Shell指令 在C语言中&#xff0c;有几种方法可以执行Shell指令&#xff1a; 1. 使用system()函数 这是最简单的方法&#xff0c;包含在stdlib.h头文件中&#xff1a; #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

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 解决方案&…...

【VLNs篇】07:NavRL—在动态环境中学习安全飞行

项目内容论文标题NavRL: 在动态环境中学习安全飞行 (NavRL: Learning Safe Flight in Dynamic Environments)核心问题解决无人机在包含静态和动态障碍物的复杂环境中进行安全、高效自主导航的挑战&#xff0c;克服传统方法和现有强化学习方法的局限性。核心算法基于近端策略优化…...

Chromium 136 编译指南 Windows篇:depot_tools 配置与源码获取(二)

引言 工欲善其事&#xff0c;必先利其器。在完成了 Visual Studio 2022 和 Windows SDK 的安装后&#xff0c;我们即将接触到 Chromium 开发生态中最核心的工具——depot_tools。这个由 Google 精心打造的工具集&#xff0c;就像是连接开发者与 Chromium 庞大代码库的智能桥梁…...

32单片机——基本定时器

STM32F103有众多的定时器&#xff0c;其中包括2个基本定时器&#xff08;TIM6和TIM7&#xff09;、4个通用定时器&#xff08;TIM2~TIM5&#xff09;、2个高级控制定时器&#xff08;TIM1和TIM8&#xff09;&#xff0c;这些定时器彼此完全独立&#xff0c;不共享任何资源 1、定…...

高端性能封装正在突破性能壁垒,其芯片集成技术助力人工智能革命。

2024 年&#xff0c;高端封装市场规模为 80 亿美元&#xff0c;预计到 2030 年将超过 280 亿美元&#xff0c;2024-2030 年复合年增长率为 23%。 细分到各个终端市场&#xff0c;最大的高端性能封装市场是“电信和基础设施”&#xff0c;2024 年该市场创造了超过 67% 的收入。…...