Android开发登录页面及用户登陆过一次,下次将不再显示登录页面

1.示例代码(登录页面)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="50dp"
        android:background="@color/white"
        android:paddingBottom="40dp"
        android:paddingTop="40dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="S M A R T C I T Y"
            android:textColor="@color/Dark_blue"
            android:textSize="15sp"
            android:layout_marginTop="40dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="智慧城市"
            android:textColor="@color/deep_blue"
            android:textSize="25sp"/>

        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入账号"
            android:maxLength="20"
            android:paddingLeft="20dp"
            android:layout_marginTop="40dp"/>

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入密码"
            android:maxLength="20"
            android:password="true"
            android:paddingLeft="20dp"
            />

        <TextView
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="暂无账号,立即注册"
            android:textSize="15sp"
            android:paddingBottom="20dp"
            android:gravity="center"
            android:paddingTop="20dp"
            android:layout_marginTop="20dp"
            />

    </LinearLayout>



    <Button
        android:id="@+id/sign_up"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="15sp"
        android:gravity="center"
        android:background="@color/deep_blue"
        android:textColor="@color/white"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        />

</LinearLayout>
package com.example.main1;

import androidx.appcompat.app.AppCompatActivity;

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

public class LoginActivity extends AppCompatActivity {

    private Button sign_up;
    private EditText username,password;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findcheck();
        sign_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (username.getText().toString().equals("loginXl")&&password.getText().toString().equals("123456")){

                    Intent intent=new Intent(LoginActivity.this, FoodActivity2.class);
                    startActivity(intent);
                    SharedPreferences sharedPreferences=getSharedPreferences("MyPrefs",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putBoolean("islogin",true);
                    editor.apply();
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    private void findcheck(){
        sign_up=findViewById(R.id.sign_up);
        username=findViewById(R.id.username);
        password=findViewById(R.id.password);
    }
}

2.启动页面


        SharedPreferences sharedPreferences=getSharedPreferences("MyPrefs",MODE_PRIVATE);
        boolean isLoggedIn = sharedPreferences.getBoolean("islogin", false);
        if (!isLoggedIn) {
            // 用户未登录,跳转到登录界面
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        }

3.效果图

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小舒卿雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值