欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android——Fragment 新手教学第三章 继承篇

程序员文章站 2022-07-16 09:35:47
...

上一篇:Android——Fragment 新手教学第二章 使用篇

 

本章节我们来学习一下继承篇 我们跟着上一篇继续来增加功能,以 GT_Fragment 在我的页面来 实现一个登录的小模块

先上效果图:

Android——Fragment 新手教学第三章 继承篇

 

第一步:老规矩咋们先写好 xml 布局

登录页面的布局文件:fragment_login.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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="80dp"
        android:background="#74D1FB"
        android:orientation="vertical"
        android:gravity="center"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录页面"
            android:textSize="38sp"
            />

       <EditText
           android:id="@+id/et_userName"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:hint="请填写账号"
           android:gravity="center"
           android:layout_margin="10dp"
           />

        <EditText
            android:id="@+id/et_passWord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请填写密码"
            android:gravity="center"
            android:layout_margin="10dp"
            />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

登录成功的信息布局文件:fragment_my_data.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="#68A6D6"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:orientation="vertical"
        android:gravity="center"
        >
        <TextView
            android:id="@+id/tv_userName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的用户名:"
            android:textSize="30sp"
            />
        <Button
            android:id="@+id/btn_close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭当前页面"
            android:textSize="30sp"
            android:gravity="center"
            />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

第二步:创建 Fragment 类:

创建登录类 Fragment_Login.java

//第一步:继承 BaseFragments
public class Fragment_Login extends GT.GT_Fragment.BaseFragments {

    //模拟登录的账号密码
    private String defaultValueUserName = "111";
    private String defaultValuePassWord = "222";

    private EditText et_userName;
    private EditText et_passWord;
    private Button btn_login;

    @Override
    protected int loadLayout() {
        return R.layout.fragment_login;//加载器布局
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        // 初始化 参数
        et_userName = view.findViewById(R.id.et_userName);
        et_passWord = view.findViewById(R.id.et_passWord);
        btn_login = view.findViewById(R.id.btn_login);
    }

    @Override
    protected void loadData() {
        super.loadData();

        //登录按钮
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String userName = et_userName.getText().toString();
                String passWord = et_passWord.getText().toString();

                if (userName.length() == 0 || passWord.length() == 0) {
                    toast(activity, "登录的账号密码不能为空");
                }else{

                    //判定账号和密码是否正确
                    if(!defaultValueUserName.equals(userName) || !defaultValuePassWord.equals(passWord)){
                        toast(activity,"登录的账号或密码错误!");
                        return;
                    }

                    toast(activity,"登录成功!");
                    //传递 Bundle 数据
                    Bundle bundle = new Bundle();
                    bundle.putString("userName", userName);
                    bundle.putString("passWord", passWord);
                    //跳转 Fragment
                    startFragment(Fragment_MyData.newInstance(bundle));
                }

            }
        });

    }
}

创建登录成功类 Fragment_MyData.java

//第一步:继承 BaseFragments
public class Fragment_MyData extends GT.GT_Fragment.BaseFragments {

    private Button btn_close;
    private TextView tv_userName;

    public static Fragment_MyData newInstance(Bundle bundle) {
        if(bundle == null){
            bundle = new Bundle();
        }
        Fragment_MyData fragment = new Fragment_MyData();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int loadLayout() {
        return R.layout.fragment_my_data;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {

        //初始化 UI
        btn_close = view.findViewById(R.id.btn_close);
        tv_userName = view.findViewById(R.id.tv_userName);

    }

    @Override
    protected void loadData() {
        super.loadData();

        Bundle bundle = getArguments();
        if(bundle != null){
            String userName = bundle.getString("userName","账号获取异常");
            tv_userName.setText("我的用户名:" + userName);
        }

        btn_close.findViewById(R.id.btn_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast(activity, "按下关闭");
                finish();//关闭当前 Fragment
            }
        });
    }

    @Override
    protected boolean onBackPressed() {
        /**
         * 监听返回键
         * 返回 true : 截取返回事件 不允许返回
         * 返回 false: 可以返回
         */
        toast(activity, "按下了返回键");

        return true;
    }

}

第三步:在登录页面写好跳转到登录界面的代码

跳转代码:Fragment_My.java

public class Fragment_My extends GT.GT_Fragment.BaseFragments {

    @Override
    protected int loadLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        view.findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast(activity,"跳转到登录页面");
                startFragment(Fragment_Login.class);//跳转到 登录页面
            }
        });
    }
}

 

下载本篇章节源码:https://github.com/1079374315/InformationDome

 

下一章:Android——Fragment 晋级教学第四章 启动模式篇

 

 

 

 

 

 

上一篇: 货仓选址

下一篇: HDU-3237-Help Bubu