OnClick linstener使用Butterknife无法正常工作

时间:2016-10-23 14:16:41

标签: android butterknife

OnClick linstener使用Butterknife 无法正常工作当我点击按钮时它没有发生任何事情,有人请检查下面的代码并建议我做任何修改。

  

MainActivity.java

package com.example.niranjan.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button)
    Button button;
    @BindView(R.id.edit)
    EditText edit;
    @BindView(R.id.text)
    TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
    @OnClick(R.id.button)
    public void onClick(View view)
    {
        String text1 = edit.getText().toString();
        text.setText(text1);
    }
}
  

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.niranjan.sample.MainActivity">
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="addtext" />

</LinearLayout>
  

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.example.niranjan.sample"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.jakewharton:butterknife:8.4.0'
    testCompile 'junit:junit:4.12'

}

3 个答案:

答案 0 :(得分:1)

我完成了以下步骤并成功实施了黄油刀

在build.gradle(项目)

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

应用&#39; android-apt&#39;和build.gradle(app)中的依赖关系

 apply plugin: 'android-apt'
        android {
          ...
        }

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

答案 1 :(得分:1)

我遇到了同样的问题,请在build.gradle(Module.app)

中尝试
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

然后     apt'com.jakewharton:butterknife-compiler:8.4.0'

也不要忘记

build.gradle(Project)中的

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

答案 2 :(得分:0)

你需要在build.gradle中的apply plugin下添加apt'com.jakewharton:butterknife-compiler:8.4.0'

同时添加

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

在buildScript-&gt;依赖项

中的项目build.gradle中