在写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. ios awakeFromNib 和 initWithCoder:

    During the instantiation process, each object in the archive is unarchived and then initialized with ...

  2. H5 前端页面适配响应式

    辞职有半个月了,面试了几家公司,还在挣扎中.... 不废话,H5页面适配成响应式,可以用百分比或者rem. rem是相对于html根元素的单位,可以根据根元素的大小做出等比缩放, 通常,假如设置,ht ...

  3. WP开发资源

    wp开发:连续两次点击返回键退出程序的设计: http://hi.baidu.com/youngytj/item/6be317719cc371306cc37ce4 X http://www.cnblo ...

  4. 构建新版本APP上传流程

    //既然是构建新版本的 APP,那么所有的证书都是已经存在了的,因此以下不再重复描述证书申请过程,如有需要请自行 Google 相关流程. 1)更改版本号: 2)去掉工程内的全局断点,同时关闭僵尸调试 ...

  5. supersr--NSURLConnection iOS2.0苹果原生请求

    get请求1:   NSURL*url = [NSURLURLWithString:@"http://127.0.0.1/demo.json"];        NSURLRequ ...

  6. September 21st 2016 Week 39th Wednesday

    Don't try so hard, the best things come when you least expect them. 不要着急,最好的总会在最不经意的时候出现. Always tur ...

  7. 关于Visual Studio 2013 编译 multi-byte character set MFC程序出现 MSB8031 错误的解决办法

    转自:http://blog.csdn.net/xiaochunzao/article/details/16987703 Visual Studio 2013 编译旧的 multi-byte char ...

  8. Oulipo(poj 3461)

    题意:求b这个字符串在a中出现的次数 这里不用KMP,用hash #include<cstdio> #include<iostream> #include<cstring ...

  9. 创建INnodb的compress表

    需要将innodb_file_per_table=1 ,innodb_file_format=Barracuda;; 如: Creating a Compressed Table in a Gener ...

  10. 数据存储--sqlite总结

    SQLite SQLite(轻量级的数据库,关系型数据库) 辅助工具:Navicat Premium 等 原理:ios针对存储问题封装了sqlite数据库(c语言数据库). 1 app获取沙盒地址命名 ...