简体   繁体   English

ButterKnife onclick无效

[英]ButterKnife onclick is not working

I injected views perfectly using butterknife library. 我使用butterknife库完美地注入了视图。 But when I try to implement listeners, for example onclick I'm not able to implement them. 但是当我尝试实现监听器时,例如onclick我无法实现它们。 Following java code will help you to understand my problem. 以下java代码将帮助您了解我的问题。

Java code: Java代码:

public class LoginActivity extends ActionBarActivity{
    @InjectView(R.id.toolbar) Toolbar toolbar;
    @InjectView(R.id.btn_login) Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.inject(this);

        initialize();
        //initListeners();

        @OnClick(R.id.btn_login)
        public void submit(View view) {
          // TODO submit data to server...
        }
    }

    /*private void initListeners() {
        @OnClick(R.id.btn_login)
        public void login(){

        }
    }*/

    private void initialize() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.toolbar_icon);
        getSupportActionBar().setTitle(null);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}

Tell me why it is happening. 告诉我它为什么会这样。 Anything wrong in code? 代码有什么问题吗? I've already configured the IDE which is compatible with ButterKnife by using following URL. 我已经使用以下URL配置了与ButterKnife兼容的IDE。

http://stackoverflow.com/questions/27754811/onclick-is-not-working-in-implementation-of-butterknife-library http://stackoverflow.com/questions/27754811/onclick-is-not-working-in-implementation-of-butterknife-library

Please give me any suggestions regarding this issue. 请就此问题向我提出任何建议。 Thanks in Advance.. 提前致谢..

MainActivity.java MainActivity.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


public class MainActivity extends ActionBarActivity {


    @InjectView(R.id.button)
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);


    }


    @OnClick(R.id.button)
    void submitButton(View view) {
            Toast.makeText(this, "Click", Toast.LENGTH_SHORT).show();
    }
}

and the activity_main.xml part 和activity_main.xml部分

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp"
        android:layout_marginTop="132dp"
        />
</RelativeLayout>

in build.gradle file(app) 在build.gradle文件(app)中

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.jakewharton:butterknife:6.1.0'
}

In my case this is my solution 就我而言,这是我的解决方案

add classpath in gradle( Project ) 在gradle中添加classpathProject

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

and gradle( Module ) add apply and apt 和gradle( 模块 )添加applyapt

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

file java 文件java

@OnClick(R.id.btn_login)
public void submit(View view) {
  // TODO submit data to server...
}

You should've to bind butterKnife before you use the annotations. 你应该在使用注释之前绑定butterKnife。

Add these dependencies to gradle.build 将这些依赖项添加到gradle.build

compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'

Then add bind to onCreate ButterKnife.bind(this); 然后将bind添加到onCreate ButterKnife.bind(this);

Now do the code to Button. 现在执行Button代码。 The method should be public and in butterKnife, you not required to add the onClick in XML. 该方法应该是公共的,并且在butterKnife中,您不需要在XML中添加onClick。 And method also should be outside of onCreate It'll automatically get button which you assign using the annotation given at above the method, 方法也应该在onCreate之外它会自动获取你使用上面方法给出的注释分配的按钮,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}
@OnClick(R.id.btn_login)
public void submit(View view){ 

   //Do your code here. 

}

make sure you add all required dependencies 确保添加所有必需的依赖项

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

You have to move your @OnClick out of the onCreate method, as i did below in the code snippet. 你必须将你的@OnClick移出onCreate方法,就像我在下面的代码片段中所做的那样。

The code i posted below should work as it's supposed to (I also use ButterKnife ). 我在下面发布的代码应该按照预期的方式工作(我也使用ButterKnife )。

public class LoginActivity extends ActionBarActivity{
    @InjectView(R.id.toolbar) Toolbar toolbar;
    @InjectView(R.id.btn_login) Button login;

    @OnClick(R.id.btn_login)
    public void submit(View view) {
       // TODO submit data to server...
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.inject(this);

        initialize();
    }

    private void initialize() {
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.toolbar_icon);
        getSupportActionBar().setTitle(null);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM