3

最近刚接触到黄油刀。我在我的 gradle(module : app) 文件中添加了这一行: compile 'com.jakewharton:butterknife:7.0.1'

它同步没有任何错误。我可以将“butterknife.Butterknife”导入到我的类文件中,导入通常的位置。但是不能导入butterknife.InjectView 似乎不存在?有什么建议么?

4

3 回答 3

5

@InjectView不再可用并由 代替@BindView。我们必须导入Butterknife依赖项才能使用注释。更多关于黄油刀的信息:- http://jakewharton.github.io/butterknife/

@BindView注释可以实现为:-

@BindView(R.id.button_id)

请注意,您将需要调用main 活动ButterKnife.bind(this);onCreate()方法来启用 Butterknife 注释。这个实现的一个例子可能是这样的: -

public class MainActivity extends AppCompatibilityActivity{
    @BindView(R.id.editText_main_firstName)
    EditText firstName;
    @BindView(R.id.editText_main_lastName)
    EditText lastName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Needs to be called to enable Butterknife annotations
        ButterKnife.bind(this);

    }
}

如果您Butterknife在片段中使用,则使用Butterknife.bind(this,view)作为片段视图的视图,即:-

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_other_product_category, container, false);
    ButterKnife.bind(this, view);
    return view;
}
于 2016-11-11T04:12:37.897 回答
5

Butterknife 7.0.0 版本包括对注释动词重命名的重大更改。这在变更日志中突出显示并反映在网站中。

Version 7.0.0 *(2015-06-27)*
----------------------------

 * `@Bind` replaces `@InjectView` and `@InjectViews`.
 * `ButterKnife.bind` and `ButterKnife.unbind` replaces `ButterKnife.inject` 
    and `ButterKnife.reset`, respectively.
...

https://github.com/JakeWharton/butterknife/blob/f65dc849d80f6761d1b4a475626c568b2de883d9/CHANGELOG.md

在http://jakewharton.github.io/butterknife/上有一个非常好的和最新的用法介绍

这是最简单的用法:

class ExampleActivity extends Activity {
  @Bind(R.id.title) TextView title;
  @Bind(R.id.subtitle) TextView subtitle;
  @Bind(R.id.footer) TextView footer;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }
}
于 2015-07-23T15:51:53.653 回答
3

很明显@InjectView被替换了@Bind

此外,您必须调用ButterKnife.bind(this);您的onCreate().

见:http: //jakewharton.github.io/butterknife/

于 2015-07-23T10:30:03.153 回答