我在我的android studio项目中使用ButterKnife 8.0.1。下面是我的gradle文件和片段类的片段。我无法在按钮点击中看到Toast消息。但是如果我使用onCLicklistener,我就能看到Toast。
请帮助我找出我做错了什么我被卡住了
我的Gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "tdd.serveroverload.com.tdd"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.jakewharton:butterknife:8.0.1'
}
片段类
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View calcView = inflater.inflate(R.layout.content_main, container, false);
calcView.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//It Works
Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
}
});
unbinder = ButterKnife.bind(this, calcView);
return calcView;
}
@OnClick(R.id.one)
public void one(View view) {
//It Does not Works
if (view.getId() == R.id.one) {
Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
result.setText(result.getText().toString() + 1);
}
答案 0 :(得分:7)
来自Butterknife github页面:
将此项添加到项目级build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
将其添加到模块级build.gradle:
apply plugin: 'android-apt'
android {
...
}
dependencies {
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
确保行应用插件...位于文件顶部的某处。
答案 1 :(得分:1)
在片段视图上绑定click事件:
您只需添加注释即可。无需使用视图检查ID。
请更新您的代码,如下所示:
@OnClick(R.id.one)
public void one() {
Toast.makeText(getActivity(), "Click", Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), "Working", Toast.LENGTH_SHORT).show();
result.setText(result.getText().toString() + 1);
}
以下是演示程序的示例代码:
/**
* Attempts to call below method when 'Fragment Binding Button' get clicked.
*/
@OnClick(R.id.btn_fragment_binding)
void OnFragmentBindingClicked() {
loadSampleFragment();
}
有关更多信息和不同的装订,请查看以下演示:
答案 2 :(得分:1)
App Level(build.gradle)
apply plugin: 'android-apt'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}
Project Level(build.gradle)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
答案 3 :(得分:0)
相同网站https://github.com/JakeWharton/butterknife#download 请在下面添加相同的内容
dependencies {
compile 'com.jakewharton:butterknife:8.6.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
}