android - ButterKnife OnClick 侦听器在自定义视图中创建视图后不久运行
问题描述
ButterKnife 的OnClick 监听器有一个奇怪的问题。当我的自定义视图在 OnClick 下面打开时,当视图打开时,监听器会自动运行。下面的代码可以帮助您了解我的问题是什么。
public class UserMenuFragment extends LinearLayout {
@BindView(R.id.buttonBlockUser)
Button buttonBlockUser;
@BindView(R.id.buttonMuteUser)
Button buttonMuteUser;
@BindView(R.id.buttonHideYourPreset)
Button buttonHideYourPreset;
@BindView(R.id.buttonTurnOnNotifs)
Button buttonTurnOnNotifs;
public UserMenuFragment(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public UserMenuFragment(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public UserMenuFragment(Context context) {
super(context);
initView();
}
private void initView() {
View view = inflate(getContext(), R.layout.fragment_user_menu, null);
addView(view);
bindViewWithButterKnife(this, view);
onButtonReportUserClicked();
onButtonBlockUserClicked();
onButtonMuteUserClicked();
onButtonHideYourPresetClicked();
onButtonCopyProfileLinkClicked();
onButtonTurnOnNotifsClicked();
}
@OnClick(R.id.buttonReportUser)
void onButtonReportUserClicked() {
Toast.makeText(getContext(), "User Reported!", Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.buttonBlockUser)
void onButtonBlockUserClicked() {
Toast.makeText(getContext(), "User Blocked!", Toast.LENGTH_SHORT).show();
changeButtonText(buttonBlockUser, R.string.unblock_user_text);
}
@OnClick(R.id.buttonMuteUser)
void onButtonMuteUserClicked() {
Toast.makeText(getContext(), "User Muted!", Toast.LENGTH_SHORT).show();
changeButtonText(buttonMuteUser, R.string.unmute_user_text);
}
@OnClick(R.id.buttonHideYourPreset)
void onButtonHideYourPresetClicked() {
Toast.makeText(getContext(), "User preset hided!", Toast.LENGTH_SHORT).show();
buttonHideYourPreset.setText(getContext().getString(R.string.show_your_preset_text));
changeButtonText(buttonHideYourPreset, R.string.show_your_preset_text);
}
@OnClick(R.id.buttonCopyProfileLink)
void onButtonCopyProfileLinkClicked() {
Toast.makeText(getContext(), "User's profile link copied!", Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.buttonTurnOnNotifs)
void onButtonTurnOnNotifsClicked() {
Toast.makeText(getContext(), "User's notification is off now!!", Toast.LENGTH_SHORT).show();
changeButtonText(buttonTurnOnNotifs, R.string.turn_off_notification_text);
}
private void changeButtonText(Button button, int newText) {
button.setText(getContext().getString(newText));
}
private void bindViewWithButterKnife(UserMenuFragment whichFragment, View view) {
ButterKnife.bind(whichFragment, view);
}
}
IMO 发生这种情况是因为我在构造函数中调用了 OnClick 侦听器,因此在创建视图(创建后)时它们自然正在运行。有人可以帮助我了解确切的问题在哪里以及如何解决它吗?
我的黄油刀版本:
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
解决方案
更改initView()
为:
private void initView() {
View view = inflate(getContext(), R.layout.fragment_user_menu, null);
addView(view);
bindViewWithButterKnife(this, view);
}
我认为您不需要调用方法,注释已经完成了设置侦听器的工作,对吗?
推荐阅读
- apache-flink - Flink 是否支持键控水印?如果没有,是否有在未来版本中实施的计划?我的替代方案是什么?
- linux - 谁能告诉我 Busybox 是如何实现自动补全的
- php - php 在 meta_value 上更改图像
- automation - 我如何下载robotium eclipse插件?
- forms - 如何在 Symfony 4 中定义一个名为“handleRequest”的未定义方法?
- javascript - Javascript轮播,显示随机div并每3秒更改一次
- c# - 按钮是可点击的,但不会进入下一页 - Selenium
- javascript - 表单提交后的状态更改在 React 中立即恢复
- vba - 如何使用 VBA 禁用另存为 Excel 97-2003 以防止绕过盐?
- angular - Mat-select 不显示选择其中一个选项的图标