通过数据库用户表,进行一个简单的用户登录窗口,并且登录成功!
下面是原代码:
package com.smart.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLite extends SQLiteOpenHelper{
public static final String TB_NAME = "smrtDataBase";
//构造方法
public SQLite(Context context) {
super(context, TB_NAME, null, 1);
}
/**
* 创建数据库表
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users (userId integer primary key autoincrement,name varchar(20),password varchar(20))");
}
/**
* 当检测与前一次创建数据库版本不一样时,先删除表再创建新表
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
package com.smart.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class UserImpl {
private SQLite sqlite;
private Context content;
private SQLiteDatabase db;
public UserImpl(Context context) {
sqlite = new SQLite(context);
}
//登录方法
public Cursor login(String name, String pwd) {
SQLiteDatabase database = sqlite.getReadableDatabase();
Cursor mCursor = database.query(true, "users", new String[] { "name" },
"name=? and password=?", new String[] { name, pwd }, null,
null, null, null);
return mCursor;
}
//保存方法
public boolean save(String name, String password) {
SQLiteDatabase database = sqlite.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("password", password);
// 必须添加记录,除了主键之处,其它都要null
database.insert("users", "name", values);
return true;
}
}
package com.smart.loginActivity;
import com.smart.db.UserImpl;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginMain extends Activity {
private static final String TAG = "LoginMain";
private Button ok;
private Button exit;
private EditText name;
private EditText password;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//得到控件
ok = (Button) findViewById(R.id.ok);
exit = (Button) findViewById(R.id.exit);
name = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
ok.setOnClickListener(new OnClickListener() {//方法编写
@Override
public void onClick(View v) {
Log.i(TAG, name.getText().toString() + "<----passord---->"
+ password.getText().toString());
try {
UserImpl impl = new UserImpl(LoginMain.this);
Cursor cursor = impl.login(name.getText().toString(), password.getText().toString());// 得到记录
if (cursor.moveToNext()) {//进行判断数据库当中是否有记得
Toast.makeText(LoginMain.this, "成功登录!", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(LoginMain.this, "你的用户名或密码错误,请重新输入",
Toast.LENGTH_LONG).show();
name.setText("");
password.setText("");
}
} catch (Exception e) {
Log.i(TAG, "报错了");
}
}
});
//取消
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, name.getText().toString() + "<----passord---->"
+ password.getText().toString());
Toast.makeText(LoginMain.this, "你点击了取消!", Toast.LENGTH_LONG)
.show();
finish();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/userName"
/>
<EditText
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="15dip"
android:id="@+id/username"
/>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/userPwd"
/>
<EditText
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="15dip"
android:id="@+id/password"
android:password="true"
/>
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/ok"
android:id="@+id/ok"
/>
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/exit"
android:id="@+id/exit"
/>
</LinearLayout>
</LinearLayout>