xUtils,butterknife...处理findviewbyid
在写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的更多相关文章
- 【Android】天气应用
模仿华为的"天气"应用写的一个小Demo.部分功能.动画效果没有实现,也没有过多考虑性能.Bug等其它方面的因素.写这个Demo的初衷是想熟悉下目前网上常用的一些框架. Demo采 ...
- Android Kotlin Annotation Processer
Annotation Processer 注解处理器(Annotation Processer)是javac内置的注解处理工具,可以在编译时处理注解,让我们自己做相应的处理.比如生成重复度很高的代码, ...
- android ButterKnife 解决重复findViewById
简介: 程序员都是懒惰的,不想写一大堆像下面这样的代码 class ExampleActivity extends Activity { TextView title; TextView subtit ...
- xUtils如何通过注解对FindViewById进行封装
之前讲到了介绍了一下xUtils的基本使用方法,今天我们就来详细介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它采用了一种注解的方式进行声明,那么我们 ...
- xUtils怎样通过注解对FindViewById进行封装
之前讲到了介绍了一下xUtils的基本用法,今天我们就来具体介绍一下关于xUtils中的ViewUtils模块. 在ViewUtils模块中我们首先看到的是它採用了一种注解的方式进行声明,那么我们首先 ...
- 厌烦了写findViewById 试试ButterKnife吧
先上官网 http://jakewharton.github.io/butterknife/ 和 https://github.com/JakeWharton/butterknife 配置开发环境 ...
- 你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
前段时间笔者在苦逼地撸代码~最后发现有些复杂的界面在写了一屏幕的findviewbyid~~~另一堆setOnXXXListener~有没有方便一点的方法让我们简单点不用每次都定义一次.find一次, ...
- Android Studio插件之快速findViewById(butterknife和Android CodeGenerator的使用)
首先在设置里面的Plugins里面下载安装插件: 安装之后会提示重启, 然后就是怎么使用了: butterknife的使用: 首先在build.gradle(app)里面添加这句话: compile ...
- 告别findViewById(),ButterKnife,使用Google Data Binding Library(1)
Data Binding Library 用数据绑定编写声名性布局,可以最大限度的减少findViewById(),setOnClickListener()之类的代码.并且比起findViewById ...
随机推荐
- 查看Android应用包名package和入口activity名称
使用android自动化测试工具启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法: 方法一:使用aapt //aa ...
- nyoj298_点的变换_错误
点的变换 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 平面上有不超过10000个点,坐标都是已知的,现在可能对所有的点做以下几种操作: 平移一定距离(M),相对X ...
- linux查看系统版本和系统位数
1. uname -a you will view kernel name.network node hostname.kernel release.kernel version.machine h ...
- oracle简历自增序列(转)
步骤:1.创建序列 2.创建触发器. 语法解析:create sequence TB_CODE_SEQUENCEminvalue 1maxvalue 99999999999999999999999 ...
- 将xml文件作为一个小的数据库,进行学生的增删改查
1.xml文件: <?xml version="1.0" encoding="UTF-8"?><Students> <studen ...
- 【leetcode】Set Matrix Zeroes(middle)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...
- POJ 2965 The Pilots Brothers' refrigerator
题目链接 题意:一个冰箱上有4*4共16个开关,改变任意一个开关的状态(即开变成关,关变成开)时,此开关的同一行.同一列所有的开关都会自动改变状态.要想打开冰箱,要所有开关全部打开才行. 输入:一个4 ...
- 单独编译osgQt模块 Qt moc
从alphapixel网站下载了OSG3.0.1VS2010x64版本的库,但是里面不包括osgQt模块,于是得自己编译 *************osgQtx64.zip工程文件可以去本博客园的“文 ...
- Linux Free命令各数字含义及Buffer和Cache的区别
Linux Free命令各数字含义及Buffer和Cache的区别 Free 命令的各数字含义 命令演示 [root@vm1 ~]# free total used free shared buffe ...
- Mysql之INFORMATION_SCHEMA解析1
INFORMATION_SCHEMA库是Mysql提供的一个系统库,保存了数据库的原数据,方便用户监控与管理Msyql. 现在单说与INNODB相关的库:INNODB_SYS_TABLES,INNOD ...