利用SQLite数据库进行存储用户名、密码等等其他信息
首先是activity_main布局界面
<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:orientation="vertical"
android:background="#74d6e6"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/banner"
android:layout_gravity="center"
android:layout_marginTop="90sp"/>
<EditText
android:id="@+id/username"
android:layout_gravity="center"
android:layout_marginTop="30sp"
android:hint="用户名"
android:layout_width="300sp"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/pasw"
android:layout_gravity="center"
android:layout_marginTop="30sp"
android:hint="密码"
android:password="true"
android:layout_width="300sp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/tijiao"
android:layout_width="300sp"
android:text="提交"
android:layout_marginTop="30sp"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/relaativelayout1"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp">
<View
android:id="@+id/view1"
android:layout_width="300sp"
android:layout_height="1sp"
android:background="#d7f2f7"
android:layout_centerHorizontal="true"
/>
<View
android:id="@+id/view2"
android:layout_width="1sp"
android:layout_height="40sp"
android:background="#d7f2f7"
android:layout_centerInParent="true"
android:layout_below="@+id/view1"/>
<TextView
android:id="@+id/zhuce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="20sp"
android:layout_toLeftOf="@+id/view2"
android:layout_marginRight="30sp"
android:layout_marginTop="10sp"
/>
<TextView
android:id="@+id/suibian"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="随便看看"
android:textSize="20sp"
android:layout_toRightOf="@+id/view2"
android:layout_marginLeft="30sp"
android:layout_marginTop="10sp"
/>
</RelativeLayout>
</RelativeLayout>
底下的两个按钮,“注册”是跳转到注册用户名和密码界面;“随便看看”是跳过注册登录页面,直接进入到主程序,方便路人进行浏览操作。
接下来是注册页面的布局zhuce.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" >
<TextView
android:id="@+id/teview1"
android:layout_width="fill_parent"
android:layout_height="70sp"
android:text="注册"
android:background="#74d6e6"
android:textSize="40sp"
android:paddingTop="10sp"
android:paddingLeft="20sp"/>
<EditText
android:id="@+id/editview1"
android:layout_marginTop="50sp"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="用户名"/>
<EditText
android:id="@+id/editview2"
android:layout_marginTop="30sp"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="密码"
android:password="true"/>
<EditText
android:id="@+id/editview3"
android:layout_marginTop="30sp"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="确认密码"
android:password="true"/>
<EditText
android:id="@+id/editview4"
android:layout_marginTop="50sp"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="电子邮箱"/>
<Button
android:id="@+id/tijiao"
android:layout_width="300sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30sp"
android:text="提交注册"/>
</LinearLayout>
其中电子邮箱的验证功能没有做,大家可以跳过这一步的填写注册
下面是MainActivity.class
package com.example.android_login;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
public class MainActivity extends Activity {
private TextView textview;
//数据库名称
private static final String DATABASE_NAME="yufengtest.db";
//数据库版本号
private static final int DATABASE_VERSION=1;
//表名
private static final String TABLE_NAME="username";
private DatebaseHelper databaseHelper;
private SQLiteDatabase db;
private Button button1;
private EditText nameText,passText;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
nameText=(EditText)findViewById(R.id.username);
passText=(EditText)findViewById(R.id.pasw);
button1=(Button)findViewById(R.id.tijiao);
textview=(TextView)findViewById(R.id.zhuce);
textview.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
intent=new Intent(MainActivity.this,zhuce.class);
startActivity(intent);
}
});
//启动注册页面
TextView textview=(TextView)findViewById(R.id.zhuce);
textview.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent a=new Intent(MainActivity.this,zhuce.class);
startActivity(a);
}
});
//启动随便看看页面
TextView textview1=(TextView)findViewById(R.id.suibian);
textview1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent a=new Intent(MainActivity.this,Main.class);
startActivity(a);
}
});
button1.setOnClickListener(new LoginListener());
}
class LoginListener implements OnClickListener{
public void onClick(View v){
String nameString =nameText.getText().toString();
String passString=passText.getText().toString();
if(nameString.equals("")||passString.equals(""))
{
//弹出消息框
new AlertDialog.Builder(MainActivity.this).setTitle("错误")
.setMessage("帐号或密码不能空").setPositiveButton("确定", null)
.show();
}else{
isUserinfo(nameString,passString);
}
}
}
public Boolean isUserinfo(String name,String pass)
{
String nameString=name;
String passString=pass;
databaseHelper=new DatebaseHelper(MainActivity.this,DATABASE_NAME,null,DATABASE_VERSION);
db = databaseHelper.getReadableDatabase();
try{
Cursor cursor=db.query(TABLE_NAME, new String[]{"name","password"},"name=?",new String[]{nameString},null,null,"password");
while(cursor.moveToNext())
{
String password=cursor.getString(cursor.getColumnIndex("password"));
if(passString.equals(password))
{
new AlertDialog.Builder(MainActivity.this).setTitle("正确")
.setMessage("成功登录").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent a=new Intent(MainActivity.this,Main.class);
startActivity(a);
}
}).show();
break;
}
else
{
Toast.makeText(this, "用户名密码不正确",Toast.LENGTH_LONG).show();
break;
}
}
}catch(SQLiteException e){
CreatTable();
}
return false;
}
private void CreatTable() {
// TODO Auto-generated method stub
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
+ " (name varchar(30) primary key,password varchar(30));";
try{
db.execSQL(sql);
}catch(SQLException ex){}
}
@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;
}
}
当然了,若想用到SQLite数据库,就必须创建一个子类DatebaseHelper.java
package com.example.android_login;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatebaseHelper extends SQLiteOpenHelper {
//数据库名称
private static final String DATABASE_NAME="yufengtest.db";
//数据库版本号
private static final int DATABASE_VERSION=1;
//数据库SQL语句 添加一个表
public DatebaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table username( name varchar(30) primary key,password varchar(30))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
注册页的zhuce.java
package com.example.android_login;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class zhuce extends Activity {
private EditText edittext1,edittext2,edittext3;
private Button button;
private DatebaseHelper databaseHelper;
//数据库名称
private static final String DATABASE_NAME="yufengtest.db";
//数据库版本号
private static final int DATABASE_VERSION=1;
private static final String TABLE_NAME="username";
private SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.zhuce);
edittext1=(EditText)findViewById(R.id.editview1);
edittext2=(EditText)findViewById(R.id.editview2);
edittext3=(EditText)findViewById(R.id.editview3);
button=(Button)findViewById(R.id.tijiao);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String namestring = edittext1.getText().toString();
String passstring = edittext2.getText().toString();
String repassstring=edittext3.getText().toString();
if(passstring.equals(repassstring))
{
databaseHelper=new DatebaseHelper(zhuce.this,DATABASE_NAME,null,DATABASE_VERSION);
db = databaseHelper.getReadableDatabase();
db.execSQL("insert into username (name,password) values(?,?)",new String[]{namestring,passstring});
Toast.makeText(zhuce.this, "注册成功!", Toast.LENGTH_LONG).show();
Intent b=new Intent(zhuce.this,MainActivity.class);
startActivity(b);
}
else
{
Toast.makeText(zhuce.this,"两次密码不一致", Toast.LENGTH_LONG).show();
}
}
});
}
}
点击dialog对话框中的“确定”按钮,会跳转到程序的主界面去,相当于直接点击“随便看看”按钮,大家可以自己编写后面的程序