当前位置: 首页 > article >正文

用java代码开发一个安卓app,实现账号注册登录

以下是一个使用Java开发的安卓App,实现账号注册和登录功能的示例代码:

1. 创建一个新的Android项目

在Android Studio中创建一个新的项目,选择"Empty Activity"模板。

2. 设计注册和登录页面的布局

res/layout目录下创建两个XML布局文件:activity_register.xmlactivity_login.xml

activity_register.xml

xml复制

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/etUsernameRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:inputType="text" />

    <EditText
        android:id="@+id/etPasswordRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/etConfirmPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="确认密码"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnRegister"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册" />

    <TextView
        android:id="@+id/tvGoToLogin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="已有账号?去登录"
        android:textSize="16sp" />

</LinearLayout>
activity_login.xml

xml复制

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <EditText
        android:id="@+id/etUsernameLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="用户名"
        android:inputType="text" />

    <EditText
        android:id="@+id/etPasswordLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />

    <TextView
        android:id="@+id/tvGoToRegister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="没有账号?去注册"
        android:textSize="16sp" />

</LinearLayout>

3. 创建注册和登录的Activity

java目录下创建两个Java类:RegisterActivity.javaLoginActivity.java

RegisterActivity.java

java复制

package com.example.userauthapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

    private EditText etUsername, etPassword, etConfirmPassword;
    private Button btnRegister;
    private TextView tvGoToLogin;

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

        etUsername = findViewById(R.id.etUsernameRegister);
        etPassword = findViewById(R.id.etPasswordRegister);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        btnRegister = findViewById(R.id.btnRegister);
        tvGoToLogin = findViewById(R.id.tvGoToLogin);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                String confirmPassword = etConfirmPassword.getText().toString().trim();

                if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请填写所有字段", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password.equals(confirmPassword)) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 这里可以添加将用户信息保存到数据库的逻辑
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

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

        tvGoToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}
LoginActivity.java

java复制

package com.example.userauthapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername, etPassword;
    private Button btnLogin;
    private TextView tvGoToRegister;

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

        etUsername = findViewById(R.id.etUsernameLogin);
        etPassword = findViewById(R.id.etPasswordLogin);
        btnLogin = findViewById(R.id.btnLogin);
        tvGoToRegister = findViewById(R.id.tvGoToRegister);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etUsername.getText().toString().trim();
                String password = etPassword.getText().toString().trim();

                if (username.isEmpty() || password.isEmpty()) {
                    Toast.makeText(LoginActivity.this, "请填写用户名和密码", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 这里可以添加从数据库验证用户信息的逻辑
                // 假设有一个简单的验证逻辑
                if (username.equals("test") && password.equals("123456")) {
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    // 跳转到主页面
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });

        tvGoToRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}

4. 创建主页面

res/layout目录下创建activity_main.xml布局文件,作为用户登录成功后的主页面。

activity_main.xml

xml复制

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎来到主页面"
        android:textSize="24sp" />

</LinearLayout>
MainActivity.java

java复制

package com.example.userauthapp;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

5. 配置AndroidManifest.xml

AndroidManifest.xml中添加Activity的声明。

xml复制

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.UserAuthApp">
        <activity android:name=".MainActivity" />
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".RegisterActivity" />
    </application>

</manifest>

6. 运行应用

在Android Studio中运行应用,你可以看到登录和注册页面,用户可以进行注册和登录操作。

注意:上述代码中的用户验证逻辑是简单的硬编码示例,在实际应用中,你需要连接到后端服务器或数据库来处理用户数据,并确保数据的安全性和完整性。


http://www.kler.cn/a/598739.html

相关文章:

  • Crow:C++高性能微服务框架的深度探索
  • SpringCloud配置中心:Config Server与配置刷新机制
  • Android 自定义变形 MD5 算法
  • linux 备份工具,常用的Linux备份工具及其备份数据的语法
  • 批处理脚本基础知识快速掌握
  • 【极速版 -- 大模型入门到进阶】快速了解大型语言模型
  • lua实现面向对象(封装/继承/多态)
  • Elasticsearch8.17 集群重启操作
  • Android应用退出后不在任务栏显示
  • Docker和Dify学习笔记
  • 什么是 “超参数” ?
  • ROS多机通信(四)——Ubuntu 网卡 Mesh 模式配置指南
  • 批量删除或修改 PPT 幻灯片页面背景
  • 美图AI增强优化版 | 功能解锁与安全部署指南
  • Redis 基础篇笔记
  • LeetCode146.LRU 缓存(哈希表+双向链表)
  • RabbitMQ 的 Ack 机制是什么?怎么合理使用它?
  • vue 对接 paypal 订阅和支付
  • 【C/C++】二叉树的最大深度(leetcode T104)bfs dfs经典例题 每日一遍
  • CE设备(Customer Edge device,用户边缘设备)