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;
@Override
protected 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() {
@Override
public void onClick(View view) {
showToast("导航栏左侧按钮被点击");
}
};
/**
* 导航栏右侧按钮点击事件
* */
View.OnClickListener topRightClick = new View.OnClickListener() {
@Override
public 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() {
@Override
public 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;
// topBar
private LinearLayout tobBarContainer;// tapBar容器
private ImageView topLeftImage;// 左侧图标
private TextView topTitle;// 标题
private ImageView topRightImage;// 右侧图标
// search
private 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);
// topBar
tobBarContainer = topView.findViewById(R.id.tobBarContainer);
topLeftImage = topView.findViewById(R.id.topBackImg);// 返回按钮
topTitle = topView.findViewById(R.id.topTitle);// 标题
topRightImage = topView.findViewById(R.id.topSearchImg);// 搜索
// searchBar
tobSearchContainer = 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);
// topBar
tobBarContainer = 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);// 右侧图标
// searchBar
tobSearchContainer = 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-->
<LinearLayout
android:id="@+id/tobBarContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:orientation="horizontal">
<ImageView
android: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"/>
<TextView
android: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"/>
<ImageView
android: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">
<!--引入顶部标题栏-->
<include
layout="@layout/top_bar"/>
*******
*******
</RelativeLayout>