根目录下新建一个类继承application,调用xUtils3初始化方法 public class AtguiguApplication extends Application { @Override public void onCreate() { super.onCreate(); //xUtils3初始化 x.Ext.init(this); // 是否输出debug日志, 开启debug会影响性能. x.Ext.setDebug(true); } } manifest配置application name <application android:name=".AtguiguApplication"
@ContentView(R.layout.activity_xutils3_main) public class XUtils3MainActivity extends Activity { @ViewInject(R.id.tv_title) private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_xutils3_main); // x.view().inject(XUtils3MainActivity.this); x.view().inject(this); //设置标题 textView.setText("xUtils3的使用"); } @Event(value = {R.id.btn_annotation,R.id.btn_net,R.id.btn_image,R.id.btn_image_list}) private void getEvent(View view){ switch (view.getId()){ case R.id.btn_annotation: // Toast.makeText(XUtils3MainActivity.this, "注解模块被点击了", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(XUtils3MainActivity.this, FragmentXUtils3Activity.class); startActivity(intent); break; case R.id.btn_net: Toast.makeText(XUtils3MainActivity.this, "网络模块被点击了", Toast.LENGTH_SHORT).show(); break; case R.id.btn_image: Toast.makeText(XUtils3MainActivity.this, "加载单张图片被点击了", Toast.LENGTH_SHORT).show(); break; case R.id.btn_image_list: Toast.makeText(XUtils3MainActivity.this, "加载列表图片被点击了", Toast.LENGTH_SHORT).show(); break; } } }
@ContentView(R.layout.activity_xutils3_fragment) public class FragmentXUtils3Activity extends FragmentActivity { @ViewInject(R.id.tv_title) private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_xutils3_fragment); x.view().inject(this); textView.setText("在Fragment中使用注解"); //1.得到FragmentManger FragmentManager fm = getSupportFragmentManager(); //2.开启事务 FragmentTransaction tt = fm.beginTransaction(); //3.替换Fragment tt.replace(R.id.fl_content,new DemoFragment()); //4.提交 tt.commit(); } }
<?xml version="1.0" encoding="utf-8"?> <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=".xutils3.annotation.FragmentXUtils3Activity"> <!--标题栏--> <include layout="@layout/titlebar" /> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
@ContentView(R.layout.fragment_demo) public class DemoFragment extends Fragment { @ViewInject(R.id.btn_fragment) private Button button; @ViewInject(R.id.tv_text) private TextView textView; /** * 上下文 */ private Context context; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context =getContext(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); return x.view().inject(this,inflater,container); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "我是按钮,我在fragment中初始化,并且被点击了", Toast.LENGTH_SHORT).show(); } }); textView.setText("我在fragment中初始化了"); } }
@ContentView(R.layout.fragment_demo) public class DemoFragment extends Fragment { @ViewInject(R.id.btn_fragment) private Button button; @ViewInject(R.id.tv_text) private TextView textView; /** * 上下文 */ private Context context; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context =getContext(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); return x.view().inject(this,inflater,container); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "我是按钮,我在fragment中初始化,并且被点击了", Toast.LENGTH_SHORT).show(); } }); textView.setText("我在fragment中初始化了"); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:textAllCaps="false" android:id="@+id/btn_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fragment的按钮" /> <TextView android:id="@+id/tv_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="text" /> </LinearLayout>
原文:https://www.cnblogs.com/znsongshu/p/9379770.html