Android简单登录系统

时间:2021-12-24 17:12:37

很长时间没有写博客了,最近一直在写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.界面截图

Android简单登录系统

Android简单登录系统

Android简单登录系统

Android简单登录系统

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注入)