ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android——GT使用教程(二十三) Annotations 教程

2021-08-05 14:03:16  阅读:245  来源: 互联网

标签:11 教程 GT 16 Fragment 5823 Android Annotations


让你在开发中爱不释手的 GT 包。关注GSLS官网,查看更多源码 ヾ(✿゚▽゚)ノ工具包。

所有文章 小编尽量让读者可以 直接 读懂 完全 复制粘贴,其中复杂或较多 的源码 会有 源码 并 贴上 github 网址

GT 类 里面的源码完全开源较多的中文注释,让更多的人直接读懂。

点个关注点个赞呗(〃'▽'〃),关注博主最新发布库: https://github.com/1079374315/GSLS_Tool

美帝 框架,让创造变得如此简单!

当你依赖GT库后就可以进行以下操作了

使用 GT 注解来开发一个 多个 Fragment 之间切换的小实例

效果图:                                                                             项目目录

                             

第一步:添加4个 fragment 的 xml 布局 与 activity_main 布局

fragment_1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F44336"
    >

</LinearLayout>

fragment_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#03A9F4"
    >

</LinearLayout>

fragment_3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4CAF50"
    >

</LinearLayout>

fragment_4.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF9800"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/ioc_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/ioc_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单击测试"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.603" />


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/ioc_tv"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/ioc_btn01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1" />

        <Button
            android:id="@+id/ioc_btn02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2" />

        <Button
            android:id="@+id/ioc_btn03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/linearLayout"
        />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        >
        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="F1"
            />
        <Button
            android:id="@+id/btn2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="F2"
            />
        <Button
            android:id="@+id/btn3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="F3"
            />
        <Button
            android:id="@+id/btn4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="F4"
            />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

第二步:添加 4 个 Fragment 类 与 编写 MainActivity 代码:

Fragment_1

public class Fragment_1 extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1,container,false);
    }

}

Fragment_2

public class Fragment_1 extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_1,container,false);
    }

}

Fragment_3

public class Fragment_3 extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_3,container,false);
    }
    
}

Fragment_4

public class Fragment_4 extends Fragment {

    @GT.Annotations.GT_View(R.id.ioc_tv)
    private TextView tv;

    @GT.Annotations.GT_View(R.id.ioc_btn)
    private Button btn;

    @GT.Annotations.GT_Object(valueString = "1079", valueInt = 21, types = {GT.Annotations.GT_Object.TYPE.STRING, GT.Annotations.GT_Object.TYPE.INT}, functions = {"setUsername", "setAge"})
    private LoginBean loginBean;

    @GT.Annotations.GT_Res.GT_String(R.string.StringTest)
    private String data;

    @GT.Annotations.GT_Res.GT_Color(R.color.colorTest)
    private int MyColor;

    @GT.Annotations.GT_Res.GT_Drawable(R.drawable.ic_launcher_background)
    private Drawable btnBG;

    @GT.Annotations.GT_Res.GT_Dimen(R.dimen.tv_size)
    private float TextSize;

    @GT.Annotations.GT_Res.GT_Animation(R.anim.alpha)
    private Animation animation;

    @GT.Annotations.GT_Res.GT_StringArray(R.array.ctype)
    private String[] strArray;

    @GT.Annotations.GT_Res.GT_IntArray(R.array.textInt)
    private int[] intArray;

    @GT.Annotations.GT_Res.GT_Layout(R.layout.activity_main)
    private View layout;

    @GT.Annotations.GT_Collection.GT_List(valueString = {"111", "222", "333"})
    private List<String> stringList;

    @GT.Annotations.GT_Collection.GT_Map(valueKey = {"username","password"},valueInt = {4444,5555})
    private Map<String,Integer> userMap;

    @GT.Annotations.GT_Collection.GT_Set(valueInt = {11,22,33,44})
    private Set<Integer> booleanSet;

    private static final String TAG = "GT_";

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_4, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        GT.getGT().build(this, view);//绑定 Fragment
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.i(TAG, "loginBean: " + loginBean);
                Log.i(TAG, "data: " + data);
                Log.i(TAG, "color: " + MyColor);
                Log.i(TAG, "Drawable: " + btnBG);
                Log.i(TAG, "TextSize: " + TextSize);
                Log.i(TAG, "animation: " + animation);
                Log.i(TAG, "strArray: " + strArray);
                Log.i(TAG, "intArray: " + intArray);
                Log.i(TAG, "layout: " + layout);
                Log.i(TAG, "stringList: " + stringList);
                Log.i(TAG, "userMap: " + userMap);
                Log.i(TAG, "booleanSet: " + booleanSet);

                tv.setText("实现成功!");
                btn.setTextColor(MyColor);
                btn.setTextSize(TextSize);
                btn.setBackgroundDrawable(btnBG);
                btn.startAnimation(animation);

            }
        });
    }

    @GT.Annotations.GT_Click({R.id.ioc_btn01, R.id.ioc_btn02, R.id.ioc_btn03})
    public void testBtnOnCLick(View view) {
        switch (view.getId()) {
            case R.id.ioc_btn01:
                Log.i(TAG, "单击 1 号");
                break;

            case R.id.ioc_btn02:
                Log.i(TAG, "单击 2 号");
                break;

            case R.id.ioc_btn03:
                Log.i(TAG, "单击 3 号");
                break;
        }
    }

}

MainActivity

public class MainActivity extends AppCompatActivity {

    //实例化 4 个 Fragment 并注入到 List 中
    @GT.Annotations.GT_Collection.GT_List({Fragment_1.class, Fragment_2.class, Fragment_3.class, Fragment_4.class})
    private List<Fragment> fragmentList;

    //定义 Fragment 管理器
    private GT.GT_Fragment gt_fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GT.getGT().build(this);//绑定 Activity

        //实例化 Fragment 管理器 并初始化要管理的 Fragment
        gt_fragment = GT.GT_Fragment.getGT_fragment()
                .initFragment(savedInstanceState, this, getSupportFragmentManager())
                .loadFragment(R.id.frameLayout, fragmentList, 0);

    }

    @GT.Annotations.GT_Click({R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4})
    public void onBtnClick(View view){
        switch (view.getId()){
            case R.id.btn1:
                gt_fragment.startFragment(Fragment_1.class);//跳转到 Fragment_1 页面
                break;
            case R.id.btn2:
                gt_fragment.startFragment(Fragment_2.class);//跳转到 Fragment_2 页面
                break;
            case R.id.btn3:
                gt_fragment.startFragment(Fragment_3.class);//跳转到 Fragment_3 页面
                break;
            case R.id.btn4:
                gt_fragment.startFragment(Fragment_4.class);//跳转到 Fragment_4 页面
                break;
        }
    }

}

效果数据:

09-16 11:16:34.657 5823-5823/? I/GT_i: ------- key:username
09-16 11:16:34.657 5823-5823/? I/GT_i: ------- key:password
09-16 11:16:40.378 5823-5823/? I/GT_: 单击 1 号
09-16 11:16:41.042 5823-5823/? I/GT_: 单击 2 号
09-16 11:16:41.619 5823-5823/? I/GT_: 单击 3 号
09-16 11:16:43.061 5823-5823/? I/GT_: loginBean: LoginBean{username='1079', password='null', age=21}
09-16 11:16:43.061 5823-5823/? I/GT_: data: 测试数据
09-16 11:16:43.061 5823-5823/? I/GT_: color: -6400
09-16 11:16:43.061 5823-5823/? I/GT_: Drawable: android.graphics.drawable.VectorDrawable@2747f5a3
09-16 11:16:43.061 5823-5823/? I/GT_: TextSize: 10.0
09-16 11:16:43.061 5823-5823/? I/GT_: animation: android.view.animation.AnimationSet@58d70a0
09-16 11:16:43.061 5823-5823/? I/GT_: strArray: [Ljava.lang.String;@3149e459
09-16 11:16:43.061 5823-5823/? I/GT_: intArray: [I@35df881e
09-16 11:16:43.061 5823-5823/? I/GT_: layout: androidx.constraintlayout.widget.ConstraintLayout{30da48ff V.E..... ......I. 0,0-0,0}
09-16 11:16:43.061 5823-5823/? I/GT_: stringList: [111, 222, 333]
09-16 11:16:43.061 5823-5823/? I/GT_: userMap: {username=4444, password=5555}
09-16 11:16:43.061 5823-5823/? I/GT_: booleanSet: [22, 44, 11, 33]

总结:只用注解只要添加 绑定操作 就可以了,如:绑定 Activity 或 绑定 Fragment 即可在 Activity 与 Fragment 内使用注解,如果 GT注解 配合 GT 注解基类 那你的代码将会得到不一样的变化。

项目源码:https://github.com/1079374315/GT_Annotation

标签:11,教程,GT,16,Fragment,5823,Android,Annotations
来源: https://blog.csdn.net/qq_39799899/article/details/105929131

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有