菜鸟的博客

纵有疾风起,人生不言弃。

导航

日报-安卓登录实现

<?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"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_01"
    android:padding="24dp">

    <!-- 顶部大标题 -->
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="桥梁智慧实验平台"
        android:textColor="@color/black"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="10dp" />


    <!-- 登录卡片 -->
    <androidx.cardview.widget.CardView
        android:id="@+id/layout_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:cardCornerRadius="16dp"
        app:cardElevation="0dp"
        android:visibility="visible"
        android:padding="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">

            <TextView
                android:text="登录"
                android:textSize="22sp"
                android:textStyle="bold"
                android:textColor="@color/black"
                android:layout_marginBottom="16dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="学号">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_login_id"
                    android:inputType="number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="密码"
                android:layout_marginTop="12dp">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/et_login_password"
                    android:inputType="textPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </com.google.android.material.textfield.TextInputLayout>

            <Button
                android:id="@+id/btn_login"
                android:text="登录"
                android:layout_marginTop="16dp"
                android:backgroundTint="#FF409EFF"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/tv_to_register"
                android:text="没有账号?点击注册"
                android:textColor="#3F51B5"
                android:textSize="14sp"
                android:layout_marginTop="12dp"
                android:layout_gravity="center_horizontal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </androidx.cardview.widget.CardView>

    <!-- 注册卡片 -->
    <androidx.cardview.widget.CardView
        android:id="@+id/layout_register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:cardCornerRadius="16dp"
        app:cardElevation="0dp"
        android:visibility="gone"
        android:padding="24dp"
        app:layout_constraintTop_toBottomOf="@id/tv_title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="100dp">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:paddingBottom="72dp">

                <TextView
                    android:text="注册"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    android:layout_marginBottom="16dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="学号">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_id"
                        android:inputType="number"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="姓名"
                    android:layout_marginTop="12dp">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="班级"
                    android:layout_marginTop="12dp">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_class"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="手机号"
                    android:layout_marginTop="12dp">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_phone"
                        android:inputType="phone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="密码"
                    android:layout_marginTop="12dp">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_password"
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="确认密码"
                    android:layout_marginTop="12dp">
                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/et_register_confirm"
                        android:inputType="textPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </com.google.android.material.textfield.TextInputLayout>

                <Button
                    android:id="@+id/btn_register"
                    android:text="注册"
                    android:layout_marginTop="16dp"
                    android:backgroundTint="#FF409EFF"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/tv_to_login"
                    android:text="已有账号?返回登录"
                    android:textColor="#3F51B5"
                    android:textSize="14sp"
                    android:layout_marginTop="12dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>
package com.di.appforbridge;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavOptions;
import androidx.navigation.Navigation;

public class LoginRegisterFragment extends Fragment {


    private View rootView;
    private CardView layoutLogin;
    private CardView layoutRegister;
    private TextView tvToRegister;
    private TextView tvToLogin;

    // 预设的账号密码
    private static final String CORRECT_ID = "20220101";
    private static final String CORRECT_PASSWORD = "1234";


    // 添加接口用于与Activity通信
    public interface OnLoginSuccessListener {
        void onLoginSuccess();
    }

    private OnLoginSuccessListener loginSuccessListener;

    // 创建Fragment的新实例
    public static LoginRegisterFragment newInstance() {
        return new LoginRegisterFragment();
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            loginSuccessListener = (OnLoginSuccessListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement OnLoginSuccessListener");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_login_register, container, false);

        initViews();
        setupClickListeners();

        return rootView;
    }

    private void initViews() {
        layoutLogin = rootView.findViewById(R.id.layout_login);
        layoutRegister = rootView.findViewById(R.id.layout_register);
        tvToRegister = rootView.findViewById(R.id.tv_to_register);
        tvToLogin = rootView.findViewById(R.id.tv_to_login);
    }

    private void setupClickListeners() {
        // 切换注册/登录视图
        tvToRegister.setOnClickListener(v -> switchToRegister());
        tvToLogin.setOnClickListener(v -> switchToLogin());

        // 登录按钮点击事件
        Button btnLogin = rootView.findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(v -> attemptLogin());

        // 注册按钮点击事件
        Button btnRegister = rootView.findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(v -> {
            Toast.makeText(getContext(), "功能暂时关闭,请联系管理员!", Toast.LENGTH_SHORT).show();
        });
    }

    private void switchToRegister() {
        layoutLogin.setVisibility(View.GONE);
        layoutRegister.setVisibility(View.VISIBLE);
    }

    private void switchToLogin() {
        layoutRegister.setVisibility(View.GONE);
        layoutLogin.setVisibility(View.VISIBLE);
    }

    private void attemptLogin() {
        // 获取输入
        EditText etId = rootView.findViewById(R.id.et_login_id);
        EditText etPassword = rootView.findViewById(R.id.et_login_password);

        String inputId = etId.getText().toString().trim();
        String inputPassword = etPassword.getText().toString().trim();

        // 验证输入
        if (inputId.isEmpty() || inputPassword.isEmpty()) {
            Toast.makeText(getContext(), "学号和密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证账号密码
        if (inputId.equals(CORRECT_ID) && inputPassword.equals(CORRECT_PASSWORD)) {
            // 登录成功,导航到主页
            Toast.makeText(getContext(), "登录成功", Toast.LENGTH_SHORT).show();
            navigateToHome();
        } else {
            Toast.makeText(getContext(), "学号或密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    private void navigateToHome() {
        // 改为调用Activity的方法
        if (loginSuccessListener != null) {
            loginSuccessListener.onLoginSuccess();
        }
    }

}

 

posted on 2025-05-12 23:54  hhmzd233  阅读(9)  评论(0)    收藏  举报