当前位置: 首页 > 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>

http://www.lryc.cn/news/297364.html

相关文章:

  • 基于鲲鹏服务器的LNMP配置
  • MIT-Missing Semester_Topic 6:Version Control (Git) 练习题
  • OpenHarmony轻量级内核-LiteOS-M
  • TCP 传输控制协议——详细
  • ArcGIS学习(六)地理数据库
  • 保研机试算法训练个人记录笔记(四)——哈希算法
  • ChatGPT实战100例 - (14) 打造AI编程助手 Code Copilot
  • 表单标记(html)
  • Linux文件和目录管理
  • 【go】gorm\xorm\ent事务处理
  • 【数据分享】1929-2023年全球站点的逐月平均风速(Shp\Excel\免费获取)
  • IP地址详解
  • Python爬虫http基本原理#2
  • Web Services 服务 是不是过时了?创建 Web Services 服务实例
  • redis单线程还快的原因
  • 【flutter】报错 cmdline-tools component is missing
  • 以用户为中心,酷开科技荣获“消费者服务之星”
  • Days 27 ElfBoard 板 AltiumDesigner 相同电路快速布局布线
  • 除夕快乐(前端小烟花)
  • fast.ai 深度学习笔记(二)
  • 风行智能电视G32Y 强制刷机升级方法,附刷机升级数据MstarUpgrade.bin
  • tsgctf-2021-lkgit-无锁竞争-userfaultfd
  • 物联网数据隐私保护技术
  • RabbitMQ-1.介绍与安装
  • CSS高级技巧
  • Redis的数据类型Hash使用场景实战
  • HttpClient | 支持 HTTP 协议的客户端编程工具包
  • DP第一天:力扣● 理论基础 ● 509. 斐波那契数 ● 70. 爬楼梯 ● 746. 使用最小花费爬楼梯
  • Android Studio 安装Flutter插件但是没法创建项目
  • 新春快乐(烟花、春联)【附源码】