很长时间没有写博客了,最近一直在写android for gis方面的项目。不过这篇博客就不写gis方面的了,今天刚刚做的一个简单的android登录系统。数据库是android自带的sqlite,sqlite的优势就不用我说了哈。下面进入正题。

1.数据库Help类

我们需要编写一个数据库辅助类来访问sqlite数据库。在数据库辅助类中,可以完成数据库的创建,表的增加、删除、修改、查询等操作。

 public class DBHelper extends SQLiteOpenHelper {

       public static final String TB_NAME = "user";
public static final String ID = "id";
public static final String NAME = "userid";
public static final String UerPwd = "userpwd";
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
this.getWritableDatabase();
// TODO Auto-generated constructor stub
} @Override
//建立表
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL("CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " ("
+ ID + " INTEGER PRIMARY KEY,"
+ NAME + " VARCHAR,"
+ UerPwd + " VARCHAR)");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub }
//关闭数据库
public void close()
{
this.getWritableDatabase().close();
}
//添加新用户
public boolean AddUser(String userid,String userpwd)
{
try
{
ContentValues cv = new ContentValues();
cv.put(this.NAME, userid);//添加用户名
cv.put(this.UerPwd,userpwd);//添加密码
this.getWritableDatabase().insert(this.TB_NAME,null,cv);
return true;
}
catch(Exception ex)
{
return false;
}
} }

DBHelper

2.登录页面

这个登录系统比较简单,我们只是简单的验证用户名和密码。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical" > <LinearLayout
android:id="@+id/lluser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_user"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/tv_loginnum"
android:textSize="18sp" /> <EditText
android:id="@+id/ed_user"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:inputType="text"
android:textSize="18sp"
android:textStyle="normal"
android:typeface="normal" > <requestFocus />
</EditText>
</LinearLayout> <LinearLayout
android:id="@+id/llpwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_pwd"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/tv_password"
android:textSize="18sp" /> <EditText
android:id="@+id/ed_pwd"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:inputType="textPassword"
android:textSize="18sp"
android:textStyle="normal" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" > <Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/txlogin" /> <Button
android:id="@+id/btnreg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:text="@string/txregister" /> </LinearLayout>
</LinearLayout> </RelativeLayout>

登录页面

这个登录界面没有任何的修饰,而且我最近喜欢用RelativeLayout和LinearLayout搭配使用。RelativeLayout是相对布局,LinearLayout是绝对布局。登录页面只有两个输入框和两个按钮,一个用于提交,另一个用于注册。

 package com.example.login;

 import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { EditText ed_id;
EditText ed_pwd;
DBHelper database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_id=(EditText)findViewById(R.id.ed_user);
ed_pwd=(EditText)findViewById(R.id.ed_pwd);
Button btnlogin=(Button)findViewById(R.id.btnlogin);
database = new DBHelper(MainActivity.this,"LoginInfo",null,1);//这段代码放到Activity类中才用this
btnlogin.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
getUser();
}
});
Button btnreg=(Button)findViewById(R.id.btnreg);
btnreg.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(MainActivity.this, UserRegister.class);
startActivity(intent);
}
});
}
public void getUser()
{
String sql="select * from user where userid=?";
Cursor cursor=database.getWritableDatabase().rawQuery(sql, new String[]{ed_id.getText().toString()});
if(cursor.moveToFirst())
{ if(ed_pwd.getText().toString().equals(cursor.getString(cursor.getColumnIndex("userpwd"))))
{
Toast.makeText(this, "登录成功", 5000).show();
}
else
{
Toast.makeText(this, "用户名或者密码错误", 5000).show();
}
}
database.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

登录界面代码

在后台,我们主要做的就是对用户名和密码的验证。通过前面定义的辅助类来实现。

3.注册页面

注册用户,提供新用户的注册。只要用户名和密码,以及对密码的确认。

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical" > <LinearLayout
android:id="@+id/lluserreg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_userreg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/tv_loginnum"
android:textSize="18sp" /> <EditText
android:id="@+id/ed_userreg"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="60dp"
android:inputType="text"
android:textSize="18sp"
android:textStyle="normal"
android:typeface="normal" > <requestFocus />
</EditText>
</LinearLayout> <LinearLayout
android:id="@+id/llpwdreg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_pwdreg"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/tv_password"
android:textSize="18sp" /> <EditText
android:id="@+id/ed_pwdreg"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="60dp"
android:inputType="textPassword"
android:textSize="18sp"
android:textStyle="normal" /> </LinearLayout> <LinearLayout
android:id="@+id/llpwdreg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_pwdreg2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/tv_conregpwd"
android:textSize="18sp" /> <EditText
android:id="@+id/ed_pwdreg2"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="22dp"
android:inputType="textPassword"
android:textSize="18sp"
android:textStyle="normal" /> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" > <Button
android:id="@+id/btnsub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/txsubmit" /> <Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:text="@string/txcancel" /> </LinearLayout>
</LinearLayout> </RelativeLayout>

用户注册

界面包括:用户名、密码、确认密码、提交和取消。

 package com.example.login;

 import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class UserRegister extends Activity { EditText edtext;
EditText edpwd;
EditText edpwd2;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.user_add);
Button btnsub=(Button)findViewById(R.id.btnsub);
edtext=(EditText)findViewById(R.id.ed_userreg);
edpwd=(EditText)findViewById(R.id.ed_pwdreg);
edpwd2=(EditText)findViewById(R.id.ed_pwdreg2);
btnsub.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
setUser();
}
});
Button btncancel=(Button)findViewById(R.id.btncancel);
btncancel.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}); }
private void setUser()
{
DBHelper database=new DBHelper(UserRegister.this,"LoginInfo",null,1); if(edtext.getText().toString().length()<=0||edpwd.getText().toString().length()<=0||edpwd2.getText().toString().length()<=0)
{
Toast.makeText(this, "用户名或密码不能为空", 5000).show();
return;
}
if(edtext.getText().toString().length()>0)
{
String sql="select * from user where userid=?";
Cursor cursor=database.getWritableDatabase().rawQuery(sql, new String[]{edtext.getText().toString()});
if(cursor.moveToFirst())
{
Toast.makeText(this, "用户名已经存在", 5000).show();
return;
}
}
if(!edpwd.getText().toString().equals(edpwd2.getText().toString()))
{
Toast.makeText(this, "两次输入的密码不同", 5000).show();
return;
}
if(database.AddUser(edtext.getText().toString(), edpwd.getText().toString()))
{
Toast.makeText(this, "用户注册成功", 5000).show();
Intent intent=new Intent();
intent.setClass(this, MainActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(this, "用户注册失败", 5000).show();
}
database.close();
} }

界面注册

后台代码主要完成用户的添加。为了确保用户的唯一性,需要对用户的账号进行验证,看表中是否已经存在相同的账号。同时,还需要确保两次输入密码的一致性。

4.界面截图

5.Sqlite相关知识

SQLiteOpenHelper是SQLiteDatabase的一个帮助类,用来管理数据库的创建和版本的更新。一般是建立一个类继承它,并实现它的onCreate和onUpgrade方法。

方法名 方法描述
SQLiteOpenHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version) 构造方法,一般是传递一个要创建的数据库名称那么参数
onCreate(SQLiteDatabase db) 创建数据库时调用
onUpgrade(SQLiteDatabase db,int oldVersion , int newVersion) 版本更新时调用
getReadableDatabase() 创建或打开一个只读数据库
getWritableDatabase() 创建或打开一个读写数据库

SQLiteDatabase类为我们提供了很多种方法,而较常用的方法如下

(返回值)方法名 方法描述
(int) delete(String table,String whereClause,String[] whereArgs) 删除数据行的便捷方法
(long) insert(String table,String nullColumnHack,ContentValues values) 添加数据行的便捷方法
(int) update(String table, ContentValues values, String whereClause, String[] whereArgs) 更新数据行的便捷方法
(void) execSQL(String sql) 执行一个SQL语句,可以是一个select或其他的sql语句
(void) close() 关闭数据库
(Cursor) query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) 查询指定的数据表返回一个带游标的数据集
(Cursor) rawQuery(String sql, String[] selectionArgs) 运行一个预置的SQL语句,返回带游标的数据集(与上面的语句最大的区别就是防止SQL注入)

Android简单登录系统的更多相关文章

  1. mvc架构的简单登录系统,jsp

    文件结构 三个jsp文件负责前段界面的实现 login.jsp <%@ page language="java" import="java.util.*" ...

  2. java单点登录系统CAS的简单使用

    转:http://blog.csdn.net/yunye114105/article/details/7997041 背景 有几个相对独立的java的web应用系统, 各自有自己的登陆验证功能,用户在 ...

  3. Springboot - 建立简单的用户登录系统

    在开始编码前,先建立几个Package(可以按个人习惯命名),如图 1.Controllers 用于存放控制器类 2.Models 用于存放数据实体类 3.Repositories 用于存放数据库操作 ...

  4. Python 做简单的登录系统

    案例 之 登录系统原创作品1 该随笔 仅插入部分代码:全部py文件源代码请从百度网盘自行下载! 链接:https://pan.baidu.com/s/1_sTcDvs5XEGDcnpoQEIrMg 提 ...

  5. Android应用与系统安全防御

    来源:HTTP://WWW.CNBLOGS.COM/GOODHACKER/P/3864680.HTML ANDROID应用安全防御 Android应用的安全隐患包括三个方面:代码安全.数据安全和组件安 ...

  6. Androidの共享登录之方案研究

    由于最近公司提到了一个需求是,一个应用登录成功了,另一个自动登录. 绞尽脑汁想了好几天,看起来很容易但是想深点就漏洞百出,有的时候代码都写完了测试都成功了突然发现给一个假设就完全失效. 先前几个同事之 ...

  7. TODO:Laravel 内置简单登录

    TODO:Laravel 内置简单登录 1. 激活Laravel的Auth系统Laravel 利用 PHP 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...

  8. 教你开发asp.net的单点登录系统

    单点登录系统,简称SSO.以下是我花了几个小时写的一个简单实现.特把实现思路和大家分享. 背景:某项目使用ASP.NET MemberShip来做会员系统,需要同时登录多个系统.而项目的开发人员无法在 ...

  9. 纯jsp用户登录系统

    用纯jsp技术实现用户登录系统,需要用到三个.jsp文件.在文本目录下新建三个.jsp文件,分别命名为login.jsp,logincl.jsp和wel.jsp. 1.login.jsp文件用来放界面 ...

随机推荐

  1. LR性能测试应用

    上半个月,由于工作和上课两边跑,几乎没有属于自己的时间去做自己想做的事,在没有加班的一天晚上,我突然冲动地跑到图书馆借了一本书<LR性能测试应用>——姜艳. 我总喜欢看那些陈旧的书,因为在 ...

  2. CSS布局方案之圣杯布局

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  3. java学习之部分笔记2

    1.变量 实例变量和局部变量 实例变量系统会自动初始化为0和null(string),局部变量必须设定初始值. 静态方法里只能引用静态变量 数据类型的自动转换! int—>long 2.构造方法 ...

  4. js取整数、取余数的方法

    1.丢弃小数部分,保留整数部分 parseInt(5/2) 2.向上取整,有小数就整数部分加1 Math.ceil(5/2) 3,四舍五入. Math.round(5/2) 4,向下取整 Math.f ...

  5. HTML5 自定义属性 data-*介绍

    在HTML5之前HTML4我们也可以自定义属性通过setAttribute去设置或者直接写在HTML标签里面那么HTML5新增data-*(*可以替换成你喜欢的任意名字)属性有什么用呢? 更便的捷操作 ...

  6. AngularJs中关于ng-class的三种使用方式说明

    在开发中我们通常会遇到一种需求:一个元素在不同的状态需要展现不同的样子. 而在这所谓的样子当然就是改变其css的属性,而实现能动态的改变其属性值,必然只能是更换其class属性 这里有三种方法: 第一 ...

  7. 如何处理JS与smarty标签的冲突

    smarty的默认标记符是大括号:{}, 假如我们页面上有JS且定义了函数或者对象,或者有CSS定义了样式,就会出现大括号, smary引擎就会把这些大括号当作smarty语法来解释, 很明显,这些C ...

  8. To and Fro(字符串水题)

    To and Fro 点我 Problem Description Mo and Larry have devised a way of encrypting messages. They first ...

  9. quoit design(hdoj p1007)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  10. MySQL load data infile

    语法: load data [low_priority] [local] infile ‘file_path' [replace] [ignore] into table table_name [(c ...