请生成上一个问题的代码

时间: 2024-02-09 22:09:25 浏览: 23
好的,这里给出一个简单的实现,仅供参考: 1. res/layout/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"> <EditText android:id="@+id/edit_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="账号" /> <EditText android:id="@+id/edit_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" /> <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" /> </LinearLayout> ``` 2. res/layout/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"> <EditText android:id="@+id/edit_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="账号" /> <EditText android:id="@+id/edit_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" /> <CheckBox android:id="@+id/check_remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住密码" /> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout> ``` 3. RegisterActivity.java (注册页面的Activity) ```java public class RegisterActivity extends AppCompatActivity { private EditText mEditAccount; private EditText mEditPassword; private Button mBtnRegister; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); mEditAccount = findViewById(R.id.edit_account); mEditPassword = findViewById(R.id.edit_password); mBtnRegister = findViewById(R.id.btn_register); mBtnRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = mEditAccount.getText().toString().trim(); String password = mEditPassword.getText().toString().trim(); SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE); if (sp.contains(account)) { Toast.makeText(RegisterActivity.this, "账号已存在", Toast.LENGTH_SHORT).show(); } else { SharedPreferences.Editor editor = sp.edit(); editor.putString(account, password); editor.apply(); Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); finish(); } } }); } } ``` 4. LoginActivity.java (登录页面的Activity) ```java public class LoginActivity extends AppCompatActivity { private EditText mEditAccount; private EditText mEditPassword; private CheckBox mCheckRemember; private Button mBtnLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mEditAccount = findViewById(R.id.edit_account); mEditPassword = findViewById(R.id.edit_password); mCheckRemember = findViewById(R.id.check_remember); mBtnLogin = findViewById(R.id.btn_login); mBtnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String account = mEditAccount.getText().toString().trim(); String password = mEditPassword.getText().toString().trim(); SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE); if (sp.contains(account) && sp.getString(account, "").equals(password)) { Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show(); if (mCheckRemember.isChecked()) { SharedPreferences.Editor editor = sp.edit(); editor.putString("last_account", account); editor.putString("last_password", password); editor.apply(); } finish(); } else { Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show(); } } }); SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE); String lastAccount = sp.getString("last_account", ""); String lastPassword = sp.getString("last_password", ""); if (!TextUtils.isEmpty(lastAccount) && !TextUtils.isEmpty(lastPassword)) { mEditAccount.setText(lastAccount); mEditPassword.setText(lastPassword); mCheckRemember.setChecked(true); } } } ``` 这个实现方式是基于SharedPreferences来保存注册的账号和密码信息,并在登录页面中进行匹配,实现注册登录功能。如果需要更高级的功能,例如加密存储用户密码等,可以参考更多的Android开发教程。

相关推荐

最新推荐

recommend-type

基于SpringBoot的代码生成器的设计和实现.doc

本文在基于减少Java Web开发者代码编写量的需求上,使用Java EE技术和Spring Boot框架设计了一个B/S模式的代码生成器系统。本设计包含了以下3个主要内容:1. 用户对数据库的实体模型进行设计和管理;2. 系统根据实体...
recommend-type

基于Mybatis plus 自动代码生成器的实现代码

本文通过实例代码给大家介绍了基于Mybatis-plus 自动代码生成器的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

MyBatis Plus中代码生成器使用详解

主要介绍了MyBatis Plus中代码生成器使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

MatlabSimulink生成CC++代码的实现

将MatlabSimulink生成CC++代码,并应用于实际的控制环境中
recommend-type

MyBatis-plus+达梦数据库实现自动生成代码的示例

主要介绍了MyBatis-plus+达梦数据库实现自动生成代码的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。