ButterKnife onclick is not working

前端 未结 5 830
梦毁少年i
梦毁少年i 2020-12-28 16:04

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

相关标签:
5条回答
  • 2020-12-28 16:15

    You should've to bind butterKnife before you use the annotations.

    Add these dependencies to 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);

    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,

    @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. 
    
    }
    
    0 讨论(0)
  • 2020-12-28 16:24

    make sure you add all required dependencies

    dependencies {
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
    }
    
    0 讨论(0)
  • 2020-12-28 16:26

    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

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

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.0.0'
        compile 'com.jakewharton:butterknife:6.1.0'
    }
    
    0 讨论(0)
  • 2020-12-28 16:29

    You have to move your @OnClick out of the onCreate method, as i did below in the code snippet.

    The code i posted below should work as it's supposed to (I also use 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-28 16:34

    In my case this is my solution

    add classpath in gradle(Project)

    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

    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

    @OnClick(R.id.btn_login)
    public void submit(View view) {
      // TODO submit data to server...
    }
    
    0 讨论(0)
提交回复
热议问题