ButterKnife 8.0.1 @OnClick 在 Fragment 中不工作 - Android Studio

ButterKnife 8.0.1 @OnClick not working in Fragment - Android Studio

我在我的 android 工作室项目中使用 ButterKnife 8.0.1。下面是我的 gradle 文件和片段 class 的片段。我无法在单击按钮时看到 Toast 消息。但是如果我使用 onCLicklistener ,我就能看到 Toast。

请帮我找出我做错了什么我卡住了

我的Gradle

apply plugin: 'com.android.application'


android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "tdd.serveroverload.com.tdd"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.jakewharton:butterknife:8.0.1'
}

片段class

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View calcView = inflater.inflate(R.layout.content_main, container, false);

        calcView.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 //It Works
                Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
            }
        });

        unbinder = ButterKnife.bind(this, calcView);

        return calcView;
    }


    @OnClick(R.id.one)
    public void one(View view) {

         //It Does not Works
        if (view.getId() == R.id.one) {

            Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
        result.setText(result.getText().toString() + 1);
    }

来自 Butterknife github 页面:

将此添加到您的项目级别 build.gradle:

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

将此添加到您的模块级别 build.gradle:

apply plugin: 'android-apt'

android {
  ...
}

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}

确保行 apply plugin ... 位于文件顶部的某个位置。

在片段视图上绑定点击事件:

您只需要添加一个注释。无需使用视图检查 ID。

请更新您的代码如下:

 @OnClick(R.id.one)
    public void one() {
        Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
        Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
        result.setText(result.getText().toString() + 1);
    }

下面是演示中的示例代码:

    /**
     * Attempts to call below method when 'Fragment Binding Button' get clicked.
     */
    @OnClick(R.id.btn_fragment_binding)
    void OnFragmentBindingClicked() {
        loadSampleFragment();
    }

有关更多信息和不同的绑定,请查看下面的演示:

https://github.com/mamatagelanee07/ButterKnifeDemo

App Level(build.gradle)

apply plugin: 'android-apt'
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'
}


Project Level(build.gradle)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {

        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

    }
}

同一网站https://github.com/JakeWharton/butterknife#download 请在下面添加相同的内容

dependencies {
  compile 'com.jakewharton:butterknife:8.6.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}