安卓简单登录

注意

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

登录

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>

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

相关推荐
Libraeking35 分钟前
破壁行动:在旧项目中丝滑嵌入 Compose(混合开发实战)
android·经验分享·android jetpack
市场部需要一个软件开发岗位1 小时前
JAVA开发常见安全问题:Cookie 中明文存储用户名、密码
android·java·安全
JMchen1233 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs4 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob4 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔4 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9964 小时前
flutter和Android动画的对比
android·flutter·动画
lxysbly6 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首6 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19439 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed