1. ViewUtils 作用:
- 完全注解方式就可以进行UI绑定和事件绑定。
- 无需findViewById和setClickListener等。
2. UI绑定 和 事件绑定
(1)UI绑定
下面我們看下具體的代碼,順便對比下注解的方式绑定ID和findViewById之間的差別:
注解的方式绑定ID:
@ViewInject(R.id.btn)
private Button btn; @ViewInject(R.id.img)
private ImageView img; @ViewInject(R.id.list)
private ListView list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ViewUtils.inject(this);
}
使用findViewById绑定ID:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); btn = (Button) findViewById(R.id.btn);
img = (ImageView) findViewById(R.id.img);
list = (ListView) findViewById(R.id.list); }
如果項目中得Activity中的控件相當多,那麼想象一下代碼中累積的那一大坨就真心受不了。通過xUtils的簡單注解,就能輕松擺脫無盡壞味道的代碼。
注意:在使用注解绑定控件的時候,一定記得在onCreate中調用ViewUtils.inject(this);
(2)事件绑定
下面我們看下具體的代碼,順便對比下两种事件绑定:
使用ViewUtils:
@OnClick({ R.id.btn, R.id.img })
public void clickMethod(View v) {
Toast.makeText(SecondActivity.this, "you clicked button!",
Toast.LENGTH_SHORT).show();
} @OnItemClick(R.id.list)
public void itemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(SecondActivity.this, "position--->" + position,
Toast.LENGTH_SHORT).show();
}
平常Android的事件绑定:
btn.setOnClickListener(this); list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(SecondActivity.this, "position--->" + position,
Toast.LENGTH_SHORT).show();
}
}); @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
Toast.makeText(SecondActivity.this, "you clicked button!",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
3. ViewUtils 基本使用:
(1)新建一个Android工程,命名为"ViewUtils",如下:
(2)首先我们来到布局文件之中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.himi.viewutils.MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1" /> <Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2" />
</LinearLayout> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>
(3)接着来到MainActivity,如下:
package com.himi.viewutils; import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends Activity { private String[] data = {
"吕布","赵云","关羽","张飞","许褚","马超"
}; /**
* 注入View到Activity
*/ @ViewInject(R.id.button1)
private Button button1; @ViewInject(R.id.button2)
private Button button2; @ViewInject(R.id.lv)
private ListView lv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//不能少
ViewUtils.inject(this); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data);
lv.setAdapter(adapter); } @OnClick({R.id.button1,R.id.button2})
public void click(View v) {//点击方法名可以随意取
if(v.getId() == R.id.button1) {
Toast.makeText(this, "点击了按钮1", 0).show();
}
if(v.getId() == R.id.button2) {
Toast.makeText(this, "点击了按钮2", 0).show();
}
} @OnItemClick(R.id.lv)//item点击方面名随便取
public void itemClick(AdapterView<?> parent, View view,
int position,long id) {
Toast.makeText(this, "你点击了按钮了 "+data[position], 0).show();
} }
上面没有使用到 网络 和 SD卡,没有必要添加权限。
(4)布署程序到手机上,如下:
4. 上面使用到的是注入View 到Activity,其实还有很多种注入View的方式,如下:
// xUtils的view注解要求必须提供id,以使代码混淆不受影响。
@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) { // 方法签名必须和接口中的要求一致
...
}
...
//在Activity中注入:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewUtils.inject(this); //注入view和事件
...
textView.setText("some text...");
...
}
//在Fragment中注入:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加载fragment布局
ViewUtils.inject(this, view); //注入view和事件
...
}
//在PreferenceFragment中注入:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件
...
}
// 其他重载
// inject(View view);
// inject(Activity activity)
// inject(PreferenceActivity preferenceActivity)
// inject(Object handler, View view)
// inject(Object handler, Activity activity)
// inject(Object handler, PreferenceGroup preferenceGroup)
// inject(Object handler, PreferenceActivity preferenceActivity)