安卓简单登录

注意

有的朋友不知道登录咋写,这里我就简单给出相应代码,用的本地存储,没用网络请求,有需要可以替换成想要的,废话不多上代码

登录

java 复制代码
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText input_name;
    private EditText input_pwd;
    private TextView btn_login;
    private TextView btn_register;

    private SharedPreferences sharedPreferences;

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

        btn_login = findViewById(R.id.btn_login);
        input_name = findViewById(R.id.input_name);
        input_pwd = findViewById(R.id.input_pwd);
        btn_register = findViewById(R.id.btn_register);

        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);

        btn_login.setOnClickListener(v -> {
            String username = input_name.getText().toString();
            String password = input_pwd.getText().toString();

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            } else {
                // 从SharedPreferences中读取保存的用户名和密码
                String savedUsername = sharedPreferences.getString("username", "");
                String savedPassword = sharedPreferences.getString("password", "");

                if (savedUsername.isEmpty() || savedPassword.isEmpty()) {
                    // 未注册,提示用户先进行注册
                    Toast.makeText(LoginActivity.this, "用户未注册,请先注册", Toast.LENGTH_SHORT).show();
                } else if (username.equals(savedUsername) && password.equals(savedPassword)) {
                    // 登录成功,跳转到下一个页面
                    Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                    startActivity(intent);
                } else {
                    // 登录失败,显示错误信息
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_register.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });
    }
}

布局

XML 复制代码
<?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:background="@color/white"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:orientation="vertical"
    tools:context=".LoginActivity">


    <ImageView
        android:layout_width="80dp"
        android:layout_gravity="center"
        android:layout_marginTop="120dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/input_name"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="请输入用户名"
        android:textSize="16sp"
        android:layout_marginTop="30dp"
        android:maxLines="1"
        android:inputType="text"
        android:background="@drawable/rounded_border_shape"
        android:singleLine="true"
        android:paddingLeft="10dp"
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/input_pwd"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="请输入密码"
        android:textSize="16sp"
        android:layout_marginTop="20dp"
        android:maxLines="1"
        android:background="@drawable/rounded_border_shape"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:singleLine="true"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:background="@drawable/rounded_shape"
        android:text="登录"/>


    <TextView
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:background="@drawable/rounded_shape"
        android:text="立即注册"/>
</LinearLayout>

效果

下面是注册

java 复制代码
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private EditText input_name;
    private EditText input_pwd;
    private TextView btn_login;
    private TextView btn_register;

    private SharedPreferences sharedPreferences;

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

        btn_login = findViewById(R.id.btn_login);
        input_name = findViewById(R.id.input_name);
        input_pwd = findViewById(R.id.input_pwd);
        btn_register = findViewById(R.id.btn_register);

        sharedPreferences = getSharedPreferences("user_info", Context.MODE_PRIVATE);

        btn_register.setOnClickListener(v -> {
            String username = input_name.getText().toString();
            String password = input_pwd.getText().toString();

            if (username.isEmpty() || password.isEmpty()) {
                Toast.makeText(RegisterActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
            } else {
                // 从SharedPreferences中读取保存的用户名
                String savedUsername = sharedPreferences.getString("username", "");

                if (savedUsername.equals(username)) {
                    // 用户名已存在
                    Toast.makeText(RegisterActivity.this, "用户名已存在,请直接登录", Toast.LENGTH_SHORT).show();
                } else {
                    // 保存用户名和密码到SharedPreferences
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("username", username);
                    editor.putString("password", password);
                    editor.apply();

                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                    // 跳转到登录页面
                    Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(loginIntent);
                }
            }
        });

        btn_login.setOnClickListener(v -> {
            Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(loginIntent);
        });
    }
}

对应布局

XML 复制代码
<?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:background="@color/white"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:orientation="vertical"
    tools:context=".LoginActivity">


    <ImageView
        android:layout_width="80dp"
        android:layout_gravity="center"
        android:layout_marginTop="120dp"
        android:layout_height="80dp"
        android:src="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/input_name"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="请输入用户名"
        android:textSize="16sp"
        android:layout_marginTop="30dp"
        android:maxLines="1"
        android:inputType="text"
        android:background="@drawable/rounded_border_shape"
        android:singleLine="true"
        android:paddingLeft="10dp"
        android:textColor="@color/black"/>

    <EditText
        android:id="@+id/input_pwd"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:hint="请输入密码"
        android:textSize="16sp"
        android:layout_marginTop="20dp"
        android:maxLines="1"
        android:background="@drawable/rounded_border_shape"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:singleLine="true"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:background="@drawable/rounded_shape"
        android:text="立即注册"/>

    <TextView
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:background="@drawable/rounded_shape"
        android:text="去登录"/>

</LinearLayout>

效果图

用户登录成功获取所有用户信息

java 复制代码
public class HomeActivity extends AppCompatActivity {

    private TextView textView;

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

        textView = findViewById(R.id.textView);
        getAllRegisteredUsers();
    }

    // 读取所有注册的用户信息
    private void getAllRegisteredUsers() {
        SharedPreferences sharedPrefs = getSharedPreferences("user_info", Context.MODE_PRIVATE);

        Map<String, ?> allEntries = sharedPrefs.getAll();
        JSONObject jsonObject = new JSONObject();
        for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
            try {
                jsonObject.put(entry.getKey(), entry.getValue());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        textView.setText("当前注册的所有用户信息如下\n"+jsonObject.toString());
    }
}

布局

XML 复制代码
<?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"
    tools:context=".HomeActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="登录成功"
        android:layout_marginTop="60dp"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center"
        android:text="登录成功"
        android:textSize="30sp" />
</LinearLayout>

最后加上一个rounded_border_shape.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" /> <!-- 填充颜色为白色,可以根据需要更改 -->
    <stroke
        android:width="2dp"
    android:color="#787676" />
    <corners android:radius="10dp" />
</shape>

和 rounded_shape.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#2196F3" /> <!-- 填充颜色为白色,可以根据需要更改 -->
    <stroke
        android:width="2dp"
        android:color="#2196F3" />
    <corners android:radius="10dp" />
</shape>

以上就是整个登录注册代码,感激大家支持

相关推荐
试行34 分钟前
Android实现自定义下拉列表绑定数据
android·java
Dingdangr5 小时前
Android中的Intent的作用
android
技术无疆5 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
GEEKVIP5 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
Jouzzy12 小时前
【Android安全】Ubuntu 16.04安装GDB和GEF
android·ubuntu·gdb
极客先躯13 小时前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
Good_tea_h15 小时前
Android中的单例模式
android·单例模式
计算机源码社20 小时前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
丶白泽21 小时前
重修设计模式-结构型-门面模式
android
晨春计1 天前
【git】
android·linux·git