ButterKnife OnClick 侦听器在自定义视图中创建视图后不久运行

ButterKnife OnClick listeners runs shortly after view created in custom view

我对 ButterKnife's 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 侦听器,因此在创建视图时(创建后)它们自然是 运行。有人可以帮助我了解确切的问题出在哪里以及如何解决吗?

我的 ButterKnife 版本:

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);
}

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