资讯 小学 初中 高中 语言 会计职称 学历提升 法考 计算机考试 医护考试 建工考试 教育百科
栏目分类:
子分类:
返回
空麓网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
空麓网 > 计算机考试 > 软件开发 > 后端开发 > Java

Android之登录注册——简易版

Java 更新时间: 发布时间: 计算机考试归档 最新发布

Android之登录注册——简易版

今天,我要分享给大家的是Android中常见的一个的登录注册的案例,我这里写的是简易版,如果大家有更精彩的拓展,可以自行发挥哦!

运行过程相信大家都已经心知肚明了,所以我在这里就直接发布代码了,其中有不理解的地方大家可以自行百度,也可以互相学习讨论。如有错误,麻烦大家在评论区留言,谢谢。

AndroidManifest.xml文件:

                                                                                            

布局代码:

styles.xml

        

activity_login.xml

                                                                            

activity_register.xml

                                                                                                                        

 activity_main.xml

    

Java代码:

LoginActivity.java

package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;import android.content.Intent;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.content.SharedPreferences;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class LoginActivity extends AppCompatActivity {    EditText lg_name,lg_psw;    Button btn_login;    Button btn_register;    SQLiteDatabase db;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_login);        lg_name=findViewById(R.id.lg_name);        lg_psw=findViewById(R.id.lg_psw);        btn_login=findViewById(R.id.btn_login);        btn_register=findViewById(R.id.btn_register);        //登录注册部分数据库        db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null);        try {            db.execSQL("create table user(username varchar(100),password varchar(100))");        } catch (Exception e){            e.printStackTrace();        }        SharedPreferences sharedPreferences=getSharedPreferences("user",0);        lg_name.setText(sharedPreferences.getString("lg_name",""));        lg_psw.setText(sharedPreferences.getString("lg_psw",""));        btn_login.setOnClickListener(view -> {            if (lg_name.getText().toString().equals("") || lg_psw.getText().toString().equals("")){                Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();                return;            }            @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+lg_name.getText().toString()+"'",null);            if (cursor.moveToNext()){                if (cursor.getString(1).equals(lg_psw.getText().toString())){                    SharedPreferences.Editor editor=getSharedPreferences("lg_name",0).edit();                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();                    startActivity(new Intent(LoginActivity.this,MainActivity.class));                    editor.apply();                }else {                    Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();                }            }else {                Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show();            }        });        btn_register.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this,RegisterActivity.class)));    }}

RegisterActivity.java

package com.example.wechat;import android.annotation.SuppressLint;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class RegisterActivity extends AppCompatActivity {    EditText rg_name,rg_psw;    Button btn_submit;    SQLiteDatabase db;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_register);        db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null);        rg_psw=findViewById(R.id.rg_psw);        rg_name=findViewById(R.id.rg_name);        btn_submit=findViewById(R.id.btn_submit);        btn_submit.setOnClickListener(view -> {            if (rg_name.getText().toString().equals("") || rg_psw.getText().toString().equals("")){                Toast.makeText(RegisterActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();                return;            }            @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+rg_name.getText().toString()+"'",null);            if (cursor.moveToNext()){                Toast.makeText(RegisterActivity.this, "账号已存在", Toast.LENGTH_SHORT).show();            }else {                ContentValues contentValues = new ContentValues();                contentValues.put("username", rg_name.getText().toString());                contentValues.put("password", rg_psw.getText().toString());                db.insert("user", null, contentValues);                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));            }        });    }}

MainActivity.java

package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }}

像登录注册此类问题的案例,我们要特别注意逻辑性,涉及到数据量比较多、杂的话,我们可以用

笔将它们记录在本子上,世上无难事,只怕有心人,让我们一起加油吧!!!

转载请注明:文章转载自 http://www.konglu.com/
本文地址:http://www.konglu.com/it/1098445.html
免责声明:

我们致力于保护作者版权,注重分享,被刊用文章【Android之登录注册——简易版】因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理,本文部分文字与图片资源来自于网络,转载此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请立即通知我们,情况属实,我们会第一时间予以删除,并同时向您表示歉意,谢谢!

我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2023 成都空麓科技有限公司

ICP备案号:蜀ICP备2023000828号-2