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 ...
随机推荐
- 解读Unity中的CG编写Shader系列五(理论知识)
转自 http://www.itnose.net/detail/6098474.html 经过前面的系列文章中的三个例子,尽管代码简单,但是我想应该还有些地方没有100%弄明白,我们现在得回过头来补充 ...
- iOS 关于UIWindow的理解
Every iOS app has a window that handles the presentation of the app’s user interface. Although the w ...
- SQL Server 无法在服务器上访问指定的路径或文件解决方法
SQL Server 无法在服务器上访问指定的路径或文件解决方法 在SQL Server附加数据库或备份数据库时出现:无法在服务器上访问指定的路径或文件. 请确保您具有必需的安全权限且该路径或文件存在 ...
- WebService及WCF获取客户端IP,端口
wcf获取客户端IP,端口 var context = OperationContext.Current; var properties = context.IncomingMessageProper ...
- codeforces 507B. Painting Pebbles 解题报告
题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...
- supersr--图片轮播逻辑
// // ViewController.m // 图片轮播 // // Created by apple on 14-5-18. // Copyright (c) 2014年 All rig ...
- [Android Pro] Android libdvm.so 与 libart.so
reference to :http://blog.csdn.net/koffuxu/article/details/44780351 Android libdvm.so 与 libart.so ...
- October 10th 2016 Week 42nd Monday
What makes life dreary is the want of motive. 没有了目标,生活便黯然无光. Motive and goal, are absolutely indispe ...
- !对c++类的理解
c++的类可以分为两类,一种是entity的类(i.e.,实体类),一种是function的类(i.e.,功能类). 对于构造entity的类,包括这种entity的属性已经它本身具备的功能: 而fu ...
- eclipse clear swtich workspace
edit : --> D:\tools\eclipse\configuration\.settings\org.eclipse.ui.ide.prefs