ButterKnife onclick 不起作用

时间:2023-01-15
本文介绍了ButterKnife onclick 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用黄油刀库完美地注入了视图.但是当我尝试实现侦听器时,例如 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-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();
    }
}

以及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 文件(应用程序)中

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 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:Android 一个用于多个按钮的 OnClick 方法? 下一篇:View 类型中的方法 setOnClickListener(View.OnClickListen

相关文章

最新文章