• Android开发之注解式框架ButterKnife的使用


    ButterKnife官网

    其实ButterKnife的注解式,与xUtils的ViewUtils模块基本上差不多,只要用过xUtils,这个框架基本上就会了。

    一、原理。

    最近发现一个很好用的开源框架,蛮不错的,可以简化你的代码,是关于注解的。不多说直接进入使用步骤讲解。

    二、步骤。

    1、准备阶段,先到官网( http://jakewharton.github.io/butterknife/  )上jar包,下载下来。

    2、把下载下来的jar包,放到项目的libs下,就会自动导入项目了。

    3、配置eclips,鼠标对准需要注解的项目,单击右键 poperties –>java Compiler –>

    Annotation Procession –> 钩一下 Enable project specific settings 其它的就会自动钩上了

    –> Factory Path ( 钩一下Enable project specific settings )–> 最后Add …. JARs 把刚刚下载的jar包来。这样eclips配置就可以了。

    4、以下是图片讲解。

    android_butterknife_iamge_01android_butterknife_02android_butterknife_03android_butterknife_04

    5、是用注解,直接上代码。

    xml部分

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2   xmlns:tools="http://schemas.android.com/tools"
     3   android:layout_width="match_parent"
     4   android:layout_height="match_parent" >
     5    <TextView
     6      android:id="@+id/tv_test"
     7      android:layout_width="wrap_content"
     8      android:layout_height="wrap_content"
     9      android:layout_centerHorizontal="true"
    10      android:layout_centerVertical="true"
    11      android:text="@string/hello_world"
    12      tools:context=".MainActivity" />
    13 </RelativeLayout>
    java部分
     1 package com.msquirrel.main;
     2 import butterknife.ButterKnife;
     3 import butterknife.InjectView;
     4 import butterknife.OnClick;
     5 import android.os.Bundle;
     6 import android.app.Activity;
     7 import android.widget.TextView;
     8  
     9  
    10 public class MainActivity extends Activity {
    11  
    12   @InjectView(R.id.tv_test)
    13   TextView tvTest;
    14  
    15   @Override
    16   public void onCreate(Bundle savedInstanceState) {
    17      super.onCreate(savedInstanceState);
    18      setContentView(R.layout.activity_main);
    19      ButterKnife.inject(this);
    20      tvTest.setText("test");
    21   }
    22  
    23   @OnClick(R.id.tv_test)
    24   public void sayHi() {
    25      tvTest.setText("Hello!");
    26   }
    27 }

    这样就算完成了,就可以使用注解了。

  • 相关阅读:
    函数的扩展
    数组的扩展
    event(1)
    面试
    iframes
    浏览器CSS兼容
    BFC
    简单的一个轮播效果
    asp.net identity 2.2.0 在WebForm下的角色启用和基本使用(二)
    我的web框架设计
  • 原文地址:https://www.cnblogs.com/liyiran/p/4634795.html
Copyright © 2020-2023  润新知