Java ButterKnife onclick 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29603834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ButterKnife onclick is not working
提问by malli
I injected views perfectly using butterknife library. But when I try to implement listeners, for example onclick
I'm not able to implement them. Following java code will help you to understand my problem.
我使用黄油刀库完美地注入了视图。但是当我尝试实现侦听器时,例如onclick
我无法实现它们。以下java代码将帮助您了解我的问题。
Java code:
爪哇代码:
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..
请就这个问题给我任何建议。提前致谢..
采纳答案by vinay Maneti
MainActivity.java
主活动.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 文件(应用程序)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.jakewharton:butterknife:6.1.0'
}
回答by Moonbloom
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);
}
}
回答by David Hackro
In my case this is my solution
就我而言,这是我的解决方案
add classpathin gradle(Project)
在gradle(项目)中添加类路径
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 applyand apt
和 gradle( Module) 添加apply和apt
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...
}
回答by Zeinab Abdelmawla
make sure you add all required dependencies
确保添加所有必需的依赖项
dependencies {
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}
回答by Fahry Mohammed
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'
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
Then add bind to onCreate ButterKnife.bind(this);
然后将绑定添加到 onCreate ButterKnife.bind(this);
Now do the code to Button. The method should be public and in butterKnife, you not required to add the onClick in XML. And method also should be outside of onCreate It'll automatically get button which you assign using the annotation given at above the method,
现在对 Button 执行代码。该方法应该是公共的,并且在 butterKnife 中,您不需要在 XML 中添加 onClick。并且方法也应该在 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.
}