Android ButterKnife示例

In this tutorial we’re going to discuss the Android ButterKnife tool and look into it’s usages.

在本教程中,我们将讨论Android ButterKnife工具并研究其用途。

Android ButterKnife (Android ButterKnife)

ButterKnife, Android ButterKnife Example

Android Butterknife is a view binding tool that uses annotations to generate boilerplate code for us. ButterKnife is developed by Jake Wharton at Square and is essentially used to save typing repetitive lines of code like findViewById(R.id.view) when dealing with views, thus making our code look a lot cleaner.


Android Butterknife是一种视图绑定工具,它使用注释为我们生成样板代码。 ButterKnife由Square的Jake Wharton开发,本质上用于在处理视图时保存诸如findViewById(R.id.view)类的重复代码行,从而使我们的代码看起来更findViewById(R.id.view)

ButterKnife Android依赖性 (ButterKnife Android Dependency)

To use ButterKnife in android application we need to add the following dependency to our build.gradle file.

要在Android应用程序中使用ButterKnife,我们需要将以下依赖项添加到我们的build.gradle文件中。

compile 'com.jakewharton:butterknife:6.1.0'

Android ButterKnife注入 (Android ButterKnife inject)

Before using any views, we need to inject ButterKnife by adding below code in onCreate() method of the activity.

在使用任何视图之前,我们需要通过在activity的 onCreate()方法中添加以下代码来注入ButterKnife。

ButterKnife.inject(this);

Note: When using fragments we need to specify the source of the view in the onCreateView() as below.

注意:使用片段时,我们需要在onCreateView()指定视图的源,如下所示。

View view = inflater.inflate(R.layout.sample_fragment, null);
ButterKnife.inject(this, view);

Android ButterKnife示例 (Android ButterKnife Example)

A simple activity example with ButterKnife is shown below.

下面显示一个使用ButterKnife的简单活动示例。

public class MainActivity extends Activity {  
  @InjectView(R.id.sample_text) TextView textView;
  @InjectView(R.id.sample_button) Button button;

  @Override 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    textView.setText("You can change this view accordingly");

    @OnClick(R.id.click_button) 
    void buttonClick() {  
    //..you don't even need the line @InjectView(R.id.click_button) if this button isn't being used else where
}

  }
}

In the above code snippet, @OnClick is the ButterKnife annotation that removes the need for the setOnClickListener method. The method below is automatically configured to that annotation. An argument inside the method is optional.

在上面的代码片段中, @OnClick是ButterKnife批注,它消除了对setOnClickListener方法的需要。 以下方法自动配置为该注释。 方法内部的参数是可选的。

We can specify multiple IDs in a single binding for common event handling as shown below.

我们可以在单个绑定中指定多个ID,以进行常见事件处理,如下所示。

@OnClick({ R.id.btn1, R.id.btn2, R.id.btn3 })
public void commonMethod(Button button) {
  button.setText("Text specified here would be same for all");
}

In the above code a specific type of view (Button) is automatically casted.

在上面的代码中,将自动强制转换特定类型的视图(按钮)。

An example of implementing ButterKnife in fragments is given below.

下面给出了在片段中实现ButterKnife的示例。

public class SomeFragment extends Fragment {

    @InjectView(R.id.textView) 
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.some_layout, container, false);

        ButterKnife.inject(this, rootView);

        //Work on the TextView
        someTextView.setVisibility(View.VISIBLE);
        return rootView;
    }

    @OnClick({ R.id.imageView, R.id.someTextView })
    public void doSomething() {
        //Do something when imageView or someTextView is clicked.
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //Set views to null:
        ButterKnife.reset(this);
    }
}

For a ListView itemClick the following annotation is used.

对于ListView项目 ,请使用以下注释。

@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
  // TODO ...
}

Note: An exception will be raised if the target view is not found. To suppress this exception we can add a @Nullable annotation, thereby making it an optional binding.

注意:如果找不到目标视图,则会引发异常。 为了抑制此异常,我们可以添加@Nullable批注,从而使其成为可选绑定。

This brings an end to ButterKnife in android short tutorial. For a complete example, please read Android Notification.

这就结束了Android简短教程中的ButterKnife。 有关完整示例,请阅读Android通知

Reference: ButterKnife at GitHub

参考: GitHub上的ButterKnife

翻译自: https://www.journaldev.com/10439/android-butterknife-example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值