在写android中,经常要出现大量的findviewbyid

        et_path = (EditText) findViewById(R.id.et_path);
        tv_info = (TextView) findViewById(R.id.tv_info);

在这儿介绍一下用注解的办法处理。从此告别findviewbyid。(偶尔)


**xUtils 3**
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.aaa)
    private TextView aaa;
    @ViewInject(R.id.bbb)
    private TextView bbb;
    @ViewInject(R.id.ccc)
    private TextView ccc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        x.view().inject(this);//一定不要忘记这句话。
    }

   // @Event(value = R.id.mybut,type = View.OnClickListener.class)
    @Event({R.id.buttonOn1,R.id.buttonOn2})
    private void button(View view) {
        if (view.getId() == R.id.buttonOn1) {
            aaa.setText("sss");
            bbb.setText("www");
            ccc.setText("eee");
        }else if(view.getId() == R.id.buttonOn2){
            bbb.setText("2222222");
        }
    }

----------

    <TextView
        android:id="@+id/aaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="aaaaa" />

    <TextView
        android:id="@+id/bbb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bbbbb" />

    <TextView
        android:id="@+id/ccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ccccc!" />

    <Button
        android:id="@+id/buttonOn1"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

    <Button
        android:id="@+id/buttonOn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" />

**如果你使用的是 
uXtils 2 :**

    @ViewInject(R.id.et_path)
    private EditText et_path;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //这句话一定不能少,这句话相当与上面的那句。效果一样
        ViewUtils.inject(this);
        }

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

//@ViewInject(vale=R.id.textView, parentId=R.id.parentView)
//TextView textView;

@ResInject(id = R.string.label, type = ResType.String)
private String label;

// 取消了之前使用方法名绑定事件的方式,使用id绑定不受混淆影响
// 支持绑定多个id @OnClick({R.id.id1, R.id.id2, R.id.id3})
// or @OnClick(value={R.id.id1, R.id.id2, R.id.id3}, parentId={R.id.pid1, R.id.pid2, R.id.pid3})
// 更多事件支持参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
@OnClick(R.id.test_button)
public void testButtonClick(View v) { // 方法签名必须和接口中的要求一致
    ...
}

3,如果你使用的是butterknife,这和上面的有点点不同。

 @InjectView(R.id.ok_btn) //控件对应的ID
 2     Button mBtn;
 3
 4     @InjectView(R.id.title_text)
 5     TextView mTitleTextView;
 6
 7     @Override
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.main_activity);
11
12         ButterKnife.inject(this);
13
14         //这样之后就可以直接使用变量了
15         mTitleTextView.setText("test");
16
17     }

@InjectViews({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<EditText> nameViews;

@OnClick(R.id.submit)
 public void submit() {
   // TODO submit data to server...
 }
 //还可以批量为多个控件添加为同一个响应函数:
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
  public void pickDoor(DoorView door) {
   if (door.hasPrizeBehind()) {
       //todo
   } else {
     //todo
   }
  }

注:最新版的库已经将方法名改了,当然也是兼容上面所列的例子的。最新示例如下:
class ExampleActivity extends Activity {
  @FindView(R.id.user) EditText username;
  @FindView(R.id.pass) EditText password;

  @OnClick(R.id.submit) void submit() {
    // TODO call server...
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(this);
    // TODO Use fields...
  }
}

用gradle配置的时候加入: 
compile files(‘libs/butterknife-7.0.1.jar’),自己下载或者github导入包重点内容



sssssssssssssssssss内容


public class MainActivity extends Activity {

    @Bind(R.id.tv_text1)
    TextView tv_text1;

    @Bind(R.id.tv_text2)
    TextView tv_text2;

    @Bind(R.id.tv_text3)
    TextView tv_text3;

    @Bind(R.id.tv_text4)
    TextView tv_text4;

    @Bind(R.id.tv_text5)
    TextView tv_text5;

    @Bind(R.id.tv_text6)
    TextView tv_text6;

    @Bind(R.id.tv_text7)
    TextView tv_text7;

    @Bind(R.id.tv_text8)
    TextView tv_text8;

    @BindString(R.string.hello_world) // 设置sting
    String title;

    @BindDrawable(R.drawable.ic_launcher) // 设置资源
    Drawable graphic;

    @BindColor(R.color.color_bule) // 设置颜色
    int red;

    @BindDimen(R.dimen.text_size) // 设置dimen值
    float spacers;

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

        ButterKnife.bind(MainActivity.this);//绑定。

        tv_text1.setText("1231");
        tv_text2.setText("1024");
        tv_text3.setText("5454");
        tv_text4.setText("7865");
        tv_text5.setText("9630");
        tv_text6.setText("wertw");

    }

    @OnClick(R.id.tv_text1)
    public void sayHi(TextView button) {
        button.setText("你按到我了啦!");
    }

    @SuppressLint("NewApi")
    @OnClick(R.id.tv_text2)
    public void sayHi2(TextView button) {
        button.setBackground(graphic);
    }

    @OnClick(R.id.tv_text3)
    public void sayHi3(TextView button) {
        button.setTextColor(red);
    }

    @OnClick(R.id.tv_text4)
    public void sayHi4(TextView button) {
        button.setTextSize(spacers);
    }


**4,如果你使用的是很早的版本,继承RoboActivity的 
,则:这个不要在onCreate里面写那几话。**

@InjectView(R.id.button)
    Button goButton;
    @InjectView(R.id.textview1)
    TextView textView1;
    @InjectView(R.id.textview2)
    TextView textView2;
    @InjectView(R.id.textview3)
    TextView textView3;
    @InjectView(R.id.textview4)
    TextView textView4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.injectview);
//      goButton.setOnClickListener(mGoListener);
        goButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            textView1.setText("Clicked");
            textView2.setText("Clicked");
            textView3.setText("Clicked");
            textView4.setText("Clicked");
        }
    }
 

xUtils,butterknife...处理findviewbyid的更多相关文章

  1. 【Android】天气应用

    模仿华为的"天气"应用写的一个小Demo.部分功能.动画效果没有实现,也没有过多考虑性能.Bug等其它方面的因素.写这个Demo的初衷是想熟悉下目前网上常用的一些框架. Demo采 ...

  2. Android Kotlin Annotation Processer

    Annotation Processer 注解处理器(Annotation Processer)是javac内置的注解处理工具,可以在编译时处理注解,让我们自己做相应的处理.比如生成重复度很高的代码, ...

  3. android ButterKnife 解决重复findViewById

    简介: 程序员都是懒惰的,不想写一大堆像下面这样的代码 class ExampleActivity extends Activity { TextView title; TextView subtit ...

  4. xUtils如何通过注解对FindViewById进行封装

    之前讲到了介绍了一下xUtils的基本使用方法,今天我们就来详细介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它采用了一种注解的方式进行声明,那么我们 ...

  5. xUtils怎样通过注解对FindViewById进行封装

    之前讲到了介绍了一下xUtils的基本用法,今天我们就来具体介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它採用了一种注解的方式进行声明,那么我们首先 ...

  6. 厌烦了写findViewById 试试ButterKnife吧

    先上官网 http://jakewharton.github.io/butterknife/  和 https://github.com/JakeWharton/butterknife 配置开发环境 ...

  7. 你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件

    前段时间笔者在苦逼地撸代码~最后发现有些复杂的界面在写了一屏幕的findviewbyid~~~另一堆setOnXXXListener~有没有方便一点的方法让我们简单点不用每次都定义一次.find一次, ...

  8. Android Studio插件之快速findViewById(butterknife和Android CodeGenerator的使用)

    首先在设置里面的Plugins里面下载安装插件: 安装之后会提示重启, 然后就是怎么使用了: butterknife的使用: 首先在build.gradle(app)里面添加这句话: compile ...

  9. 告别findViewById(),ButterKnife,使用Google Data Binding Library(1)

    Data Binding Library 用数据绑定编写声名性布局,可以最大限度的减少findViewById(),setOnClickListener()之类的代码.并且比起findViewById ...

随机推荐

  1. CentOS更新yum源

    CentOS更新yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载 ...

  2. [Linux]centOS7下RPM安装Perl

    1.下载rpm依赖包,依照顺序安装. perl-parent-0.225-244.el7.noarch   perl-HTTP-Tiny-0.033-3.el7.noarch   perl-podla ...

  3. Selenium FF WebDriver运行时开启firebug的2种方式

    上一次我实测FF webdriver 加载firefoxhttp://www.cnblogs.com/tobecrazy/p/3997375.html 那么问题就来了,既然能加载firebug能否在运 ...

  4. CodeForces 407B Long Path (DP)

    题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...

  5. Vim 强大的配置

    新建文件.vimrc,然后复制如下内容,并将该文件放到vim安装目录下 map <F9> :call SaveInputData()<CR> func! SaveInputDa ...

  6. iOS应用架构谈(二):View层的组织和调用方案(中)

    iOS客户端应用架构看似简单,但实际上要考虑的事情不少.本文作者将以系列文章的形式来回答iOS应用架构中的种种问题,本文是其中的第二篇,主要讲View层的组织和调用方案.中篇主要讨论MVC.MVCS. ...

  7. cell分割线宽度不满屏处理

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZ ...

  8. Maven中手动引用第三方jar包

    有些jar包在Maven库中并不支持,但我们又需要.所以就必须手动引入. 可分为三步完成: 1 ,在项目目录下创建Lib,把引入的jar包加入. 2.在pom.xml中引入dependences. 如 ...

  9. DO语句与SELECT语句,HANDLER语句

    DO语句,只执行语句不返回结果:SELECT 既执行语句也返回结果. HANDLER的效率会更好. HANDLER 语句比SELECT 语句更快: 1,HANDLER只需OPEN一次,能重用,不须每次 ...

  10. MongoDB C API

    一.编译mongodb c driver: 编译完成之后在c:\mongo-c-driver目录下有bin.include.lib三个文件夹,分别包含所需的dll..h文件.lib. 在自己的项目中引 ...