Butterknife8.0在使用的时候,有个小小的问题,不知道是设计如此还是确实存在这样的一个bug。请看我的代码:
这两个类的关系是:CloseConnectActivity是BaseActivity的子类,Butterknife是在BaseActivity中绑定的,我现在要做的是,在子类和父类中都使用Butterknife的绑定点击事件的方法来实现View的点击事件的响应,如上图所示(BaseActivity只实现iv_back的响应),在子类中,可以实现事件响应,这个我就不质疑了,但是,在父类中,View的点击事件却迟迟得到不回应。
下面是我的解决方案:把在父类中的需要响应点击事件的View单独写出来,这一就可以响应了。
@OnClick(R.id.iv_back)
public void back(View v){
// 统一处理左上角返回键
finish();
}
或许你也在使用这个注解框架,或许你也遇到了同样的问题,希望对你有所帮助。
BaseActivity源码:
public abstract class BaseActivity extends Activity {
public long mFirstTime;
/**
* 加载对话框
*/
public LoadingDialog mLoadingDialog;
/**
* 系统状态栏管理
*/
protected SystemBarTintManager mSystemBarTintManager;
private PopupWindow popupWindow;
/**
* 设置布局文件
*/
public abstract void setContentLayout();
/**
* 在实例化布局之前处理的逻辑
*/
public void beforeInitView() {
}
/**
* 在实例化布局之后处理的逻辑
*/
public abstract void afterInitView();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityUtils.addActivity(this);
// 初始化加载对话框
initLoadingDialog();
// 初始化系统状态栏
if (isUseSystemBarTint()) {
initSystemBarTint();
}
// 使用模板定义onCreate操作步骤
if (isUseTemplate()) {
setContentLayout();
//设置强制竖屏
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//初始化ButterKnife
try {
ButterKnife.bind(this);
} catch (Exception e) {
e.printStackTrace();
}
beforeInitView();
initHeader();
afterInitView();
}
}
/**
* 統一处理左上角返回
*
* @param v
*/
@OnClick(R.id.iv_back)
public void back(View v) {
if (null == v) {
return;
}
ActivityUtils.removeActivity();
onGoback();
finish();
}
@OnClick(R.id.iv_menu)
public void ivMenuClick(View view) {
// 统一处理右上角菜单
onMenuClick(findViewById(R.id.iv_menu));
}
@OnClick(R.id.tv_menu)
public void tvMenuClick(View view) {
// 统一处理右上角菜单
onMenuClick(findViewById(R.id.tv_menu));
}
/**
* 点击头部菜单
*/
protected void onMenuClick(View menu) {
}
/**
* 初始化标题栏
*/
protected void initHeader() {
try {
ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化系统状态栏
*/
protected void initSystemBarTint() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 状态栏透明 需要在创建SystemBarTintManager 之前调用。
setTranslucentStatus(true);
}
// create our manager instance after the content view is set
mSystemBarTintManager = new SystemBarTintManager(this);
// enable status bar tint
mSystemBarTintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint
mSystemBarTintManager.setNavigationBarTintEnabled(true);
// set a custom tint color for all system bars
// mSystemBarTintManager.setTintColor(getTintColor());
// set a custom navigation bar resource
// mSystemBarTintManager.setNavigationBarTintResource(R.drawable.ic_launcher);
// set a custom status bar drawable
// mSystemBarTintManager.setStatusBarTintResource(0);
setTintResource(R.color.main_bg);
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
/**
* 是否使用沉浸式状态栏
*
* @return
*/
protected boolean isUseSystemBarTint() {
return true;
}
/**
* 设置系统状态栏背景
*
* @param resId
*/
protected void setTintResource(int resId) {
if (mSystemBarTintManager != null) {
mSystemBarTintManager.setStatusBarTintResource(resId);
}
}
/**
* 初始化加载对话框
*/
protected void initLoadingDialog() {
if (null != BaseActivity.this) {
mLoadingDialog = new LoadingDialog(this);
}
}
/**
* 是否使用模板
*/
protected boolean isUseTemplate() {
return true;
}
/**
* 隐藏头部返回
*/
public void hideBack() {
try {
ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
ivBack.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置标题
*
* @param title
*/
protected void setTitle(String title) {
try {
TextView tvTitle = (TextView) findViewById(R.id.tv_title);
tvTitle.setText(title);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置标题
*
* @param resId
*/
public void setTitle(int resId) {
try {
TextView tvTitle = (TextView) findViewById(R.id.tv_title);
tvTitle.setText(resId);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getTopTitle() {
try {
TextView tvTitle = (TextView) findViewById(R.id.tv_title);
return tvTitle.getText().toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* 显示头部图标菜单
*
* @param resId
*/
public void showImageMenu(int resId) {
try {
ImageView ivMenu = (ImageView) findViewById(R.id.iv_menu);
ivMenu.setImageResource(resId);
ivMenu.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏头部图标菜单
*/
public void hideImageMenu() {
try {
ImageView ivMenu = (ImageView) findViewById(R.id.iv_menu);
ivMenu.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 显示头部文字菜单
*
* @param menu
*/
public void showTextMenu(String menu) {
try {
TextView tvMenu = (TextView) findViewById(R.id.tv_menu);
tvMenu.setText(menu);
tvMenu.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏头部文字菜单
*/
public void hideTextMenu() {
try {
TextView tvMenu = (TextView) findViewById(R.id.tv_menu);
tvMenu.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 显示加载对话框
*/
public void showLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.show();
}
}
/**
* 显示加载对话框
*/
public void showLoadingDialog(int time) {
if (mLoadingDialog != null) {
mLoadingDialog.show(time);
}
}
/**
* 隐藏加载对话框
*/
public void hiddenLoadingDialog() {
if (mLoadingDialog != null && mLoadingDialog.isShowing())
mLoadingDialog.dismiss();
}
public void setLoadingMessage(int resId) {
if (resId != 0 && mLoadingDialog != null) {
mLoadingDialog.setMessage(resId);
}
}
public void setCancelable(boolean flag) {
if (mLoadingDialog != null) {
mLoadingDialog.setCancelable(flag);
}
}
public void showView(View view) {
if (View.VISIBLE != view.getVisibility()) {
view.setVisibility(View.VISIBLE);
}
}
public void hideView(View view) {
if (View.GONE != view.getVisibility()) {
view.setVisibility(View.GONE);
}
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
}
protected void onGoback() {
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != mLoadingDialog) {
if (mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
mLoadingDialog = null;
}
ActivityUtils.removeActivity();
}
}