Android之登录注册——简易版

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

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

AndroidManifest.xml文件:

<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.WeChat">
        <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" />
        <activity android:name=".MainActivity"/>
    </application>

布局代码:

styles.xml

<style name="hLine">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">@color/black</item>
    </style>
    <style name="tvTwo">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">20dp</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">15sp</item>
    </style>
    <style name="etOne">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">30dp</item>
        <item name="android:background">@null</item>
        <item name="android:textColor">@color/black</item>
    </style>

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="用户登录/LOGIN"
        android:textSize="30sp"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:layout_marginTop="50dp"
        />
    <View
        style="@style/hLine"
        android:layout_marginTop="40dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="15dp">
        <TextView
            style="@style/tvTwo"
            android:text="性   名:" />
        <EditText
            android:id="@+id/lg_name"
            style="@style/etOne" />
    </LinearLayout>
    <View style="@style/hLine" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="15dp">
        <TextView
            style="@style/tvTwo"
            android:text="密   码:" />
        <EditText
            android:id="@+id/lg_psw"
            style="@style/etOne"
            android:inputType="textPassword"/>
    </LinearLayout>
    <View style="@style/hLine" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="50dp"
            android:text="登 录"
            android:background="@color/gray"
            />
        <Button
            android:id="@+id/btn_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="120dp"
            android:text="注 册"
            android:background="@color/gray"
            />
    </LinearLayout>
</LinearLayout>

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="用户注册"
            android:textColor="@color/black"
            android:textSize="40sp"
            />
        <View style="@style/hLine" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15dp">
            <TextView
                style="@style/tvTwo"
                android:text="姓名:" />
            <EditText
                android:id="@+id/rg_name"
                style="@style/etOne" />
        </LinearLayout>
        <View style="@style/hLine" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="15dp">
            <TextView
                style="@style/tvTwo"
                android:text="密码:" />
            <EditText
                android:id="@+id/rg_psw"
                style="@style/etOne"
                android:inputType="textPassword" />
        </LinearLayout>
        <View style="@style/hLine" />
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="120dp"
            android:gravity="center"
            android:text="提交"
            android:background="@color/gray"
            android:textSize="18sp" />
</LinearLayout>

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="祝你,登录成功,未来可期!"
        android:textColor="#E53935"
        android:textSize="30sp"/>
</LinearLayout>

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);
    }
}

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

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

  • 0
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶灼hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值