本文介绍了ButterKnife onclick无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用butterknife库完美地注入了视图。但是当我尝试实现监听器时,例如 onclick 我无法实现它们。以下java代码将帮助您理解我的问题。

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.

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

告诉我它为什么会发生。代码有什么问题吗?我已经使用以下URL配置了与ButterKnife兼容的IDE。

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.

http://stackoverflow.com/questions/27754811/onclick-is-not-正在实施的刀库 -

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

和activity_main.xml部分

and the activity_main.xml part

<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>

build.gradle文件(app)中的

in build.gradle file(app)

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

这篇关于ButterKnife onclick无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 18:11