首页 > 解决方案 > 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'

标签: androidonclicklistenerandroid-custom-viewbutterknife

解决方案


更改initView()为:

private void initView() {
    View view = inflate(getContext(), R.layout.fragment_user_menu, null);
    addView(view);
    bindViewWithButterKnife(this, view);
}

我认为您不需要调用方法,注释已经完成了设置侦听器的工作,对吗?


推荐阅读