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

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

dedecms 织梦自定义表单留言增加ajax验证码功能

增加ajax功能模块&#xff0c;用户不点击提交按钮&#xff0c;只要输入框失去焦点&#xff0c;就会提前提示验证码是否正确。 一&#xff0c;模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...

Module Federation 和 Native Federation 的比较

前言 Module Federation 是 Webpack 5 引入的微前端架构方案&#xff0c;允许不同独立构建的应用在运行时动态共享模块。 Native Federation 是 Angular 官方基于 Module Federation 理念实现的专为 Angular 优化的微前端方案。 概念解析 Module Federation (模块联邦) Modul…...

【Zephyr 系列 10】实战项目:打造一个蓝牙传感器终端 + 网关系统(完整架构与全栈实现)

🧠关键词:Zephyr、BLE、终端、网关、广播、连接、传感器、数据采集、低功耗、系统集成 📌目标读者:希望基于 Zephyr 构建 BLE 系统架构、实现终端与网关协作、具备产品交付能力的开发者 📊篇幅字数:约 5200 字 ✨ 项目总览 在物联网实际项目中,**“终端 + 网关”**是…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

PL0语法,分析器实现!

简介 PL/0 是一种简单的编程语言,通常用于教学编译原理。它的语法结构清晰,功能包括常量定义、变量声明、过程(子程序)定义以及基本的控制结构(如条件语句和循环语句)。 PL/0 语法规范 PL/0 是一种教学用的小型编程语言,由 Niklaus Wirth 设计,用于展示编译原理的核…...

ardupilot 开发环境eclipse 中import 缺少C++

目录 文章目录 目录摘要1.修复过程摘要 本节主要解决ardupilot 开发环境eclipse 中import 缺少C++,无法导入ardupilot代码,会引起查看不方便的问题。如下图所示 1.修复过程 0.安装ubuntu 软件中自带的eclipse 1.打开eclipse—Help—install new software 2.在 Work with中…...

工业自动化时代的精准装配革新:迁移科技3D视觉系统如何重塑机器人定位装配

AI3D视觉的工业赋能者 迁移科技成立于2017年&#xff0c;作为行业领先的3D工业相机及视觉系统供应商&#xff0c;累计完成数亿元融资。其核心技术覆盖硬件设计、算法优化及软件集成&#xff0c;通过稳定、易用、高回报的AI3D视觉系统&#xff0c;为汽车、新能源、金属制造等行…...

自然语言处理——Transformer

自然语言处理——Transformer 自注意力机制多头注意力机制Transformer 虽然循环神经网络可以对具有序列特性的数据非常有效&#xff0c;它能挖掘数据中的时序信息以及语义信息&#xff0c;但是它有一个很大的缺陷——很难并行化。 我们可以考虑用CNN来替代RNN&#xff0c;但是…...

根据万维钢·精英日课6的内容,使用AI(2025)可以参考以下方法:

根据万维钢精英日课6的内容&#xff0c;使用AI&#xff08;2025&#xff09;可以参考以下方法&#xff1a; 四个洞见 模型已经比人聪明&#xff1a;以ChatGPT o3为代表的AI非常强大&#xff0c;能运用高级理论解释道理、引用最新学术论文&#xff0c;生成对顶尖科学家都有用的…...