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.效果图
![](https://img-blog.csdnimg.cn/direct/1cbaf9829e984578921388d844b08691.gif)