随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、博主微信(guyun297890152)、QQ技术交流群(183198395)。
文章图片
文章图片
package com.home.testxutils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.ResType;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ResInject;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
@ContentView(R.layout.test)
public class MainActivity extends Activity { @ViewInject(R.id.test_et)
private EditText inputText;
@ViewInject(R.id.test_tv)
private TextView showText;
@ViewInject(R.id.test_btn1)
private Button testBtn1;
@ViewInject(R.id.test_btn2)
private Button testBtn2;
@ResInject(id = R.string.app_name, type = ResType.String)
private String label;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewUtils.inject(this);
} @OnClick({ R.id.test_btn1, R.id.test_btn2 })
public void click(View view) {
switch (view.getId()) {
case R.id.test_btn1:
showText.setText(label + ":点击按钮1:" + inputText.getText().toString());
break;
case R.id.test_btn2:
showText.setText(label + ":点击按钮2:" + inputText.getText().toString());
break;
default:
break;
} }}
【Xutils的注解示例】
//在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)