Loading

Android学习-用子菜单实现不同身份登录页面的切换

实现了安卓每日打卡APP教师身份的登录,在这部分我选择使用子菜单实现,效果如下:

实现代码见后

进入APP,默认是学生登录界面:

 

 打开右上角子菜单,选择跳到教师登录界面:

 

 用同样的操作可以跳转回去;如果已经是本页面还点击跳转到本来的页面,则会弹出提示:

 

 核心代码其实就是重写这两个方法:

public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.teacher_view:
                Toast.makeText(this, "已经是教师登录页面!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.student_view:
                Toast.makeText(this, "转换到学生登录页面!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

部分代码:

MainActivity
package com.example.clockappliction;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.clockappliction.DataBase.CRUD;
import com.example.clockappliction.Information.Student;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_login, btn_reg;

    private EditText et_log_id,et_log_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);

        btn_reg = (Button) findViewById(R.id.btn_reg);
        btn_reg.setOnClickListener(this);

        et_log_id = (EditText) findViewById(R.id.et_log_id);
        et_log_password = (EditText) findViewById(R.id.et_log_password);

    }

    @Override
    public void onClick(View view) {

        if (view == findViewById(R.id.btn_login)){

            Student student = new Student();
            CRUD crud = new CRUD(this);
            //获取输入框的学生信息
            student.id = et_log_id.getText().toString();
            student.password = et_log_password.getText().toString();
            //通过id获取数据库学生信息
            Student studentData =crud.getStudentById(student.id);
            //登录
            if (student.id.equals(studentData.id)){

                if (student.password.equals(studentData.password)){
                    student.name= studentData.name;
                    Intent intent =new Intent(this,MenuActivity.class);
                    intent.putExtra("st_id",student.id);
                    intent.putExtra("st_name",student.name);
                    startActivity(intent);
                }else {
                    Toast.makeText(this, "密码不正确", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "学号不正确", Toast.LENGTH_SHORT).show();
            }

        } else if (view == findViewById(R.id.btn_reg)) {
            //跳转到注册页面
            Intent intent = new Intent(this,RegActivity.class);
            intent.putExtra("keys",0);
            startActivity(intent);
        }


    }
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.teacher_view:
                Toast.makeText(this, "转换到教师登录页面!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,TeacherActivity.class);
                startActivity(intent);
                break;
            case R.id.student_view:
                Toast.makeText(this, "已经是学生登录页面!", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
TeacherActivity
package com.example.clockappliction;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.clockappliction.Information.Student;

public class TeacherActivity extends AppCompatActivity implements View.OnClickListener  {

    private Button mBtn_log;
    private EditText eText_id,eText_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher);

        mBtn_log = findViewById(R.id.btn_teacher_login);
        mBtn_log.setOnClickListener(this);

        eText_id = findViewById(R.id.et_teacher_id);
        eText_password = findViewById(R.id.et_teacher_password);

    }

    @Override
    public void onClick(View view) {

        if (view == findViewById(R.id.btn_teacher_login)){
            //获取输入框的学生信息
            Student student = new Student();
            student.id = eText_id.getText().toString();
            student.password = eText_password.getText().toString();
            if (student.id.equals("teacher")){
                if (student.password.equals("123")){
                    Intent intent = new Intent(this,InfoActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "账号错误", Toast.LENGTH_SHORT).show();
            }

        }

    }
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.teacher_view:
                Toast.makeText(this, "已经是教师登录页面!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.student_view:
                Toast.makeText(this, "转换到学生登录页面!", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,MainActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="转到教师登录页面"
        android:id="@+id/teacher_view">
    </item>
    <item android:title="转到学生登录页面"
        android:id="@+id/student_view">
    </item>

</menu>

 

posted @ 2023-03-15 20:03  冰稀饭Aurora  阅读(218)  评论(0编辑  收藏  举报