注册代码
package cn.com.day01;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class AssistantRegister extends Activity
{
static EditText userName01;
static EditText password01;
Sql db;
SQLiteDatabase sDatabase = null;
public void close()
{
/*if(mCursor!=null){
mCursor.close();
}*/
sDatabase.close();
}
private ImageButton dpress;
private ImageButton dpress2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
userName01 = (EditText)findViewById(R.id.text3);
password01 = (EditText)findViewById(R.id.text4);
this.dpress=(ImageButton)findViewById(R.id.Click_Button12);
this.dpress2=(ImageButton)findViewById(R.id.Click_Button13);
dpress.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View v)
{
db = new Sql(getApplicationContext(), "store.db", null, 1);
sDatabase = db.getWritableDatabase();
if (userName01.getText().toString().equals("")
|| password01.getText().toString().equals(""))
{
DialogDemo.builder(AssistantRegister.this, "错误信息", "请填写完整信息!");
}
else
{
int id1=0;
String select = "select max(_id) from user_info";
Cursor seCursor = sDatabase.rawQuery(select, null);
seCursor.moveToFirst();
id1 = Integer.parseInt(seCursor.getString(0));
id1 += 1;
sDatabase.execSQL("insert into user_info values('" + id1 + "','"
+ userName01 + "','" + password01 + "')"
);
DialogDemo.builder(AssistantRegister.this, "提示", "注册成功,请返回登录界面登录");
//seCursor.close();
sDatabase.close();
}
}
} );
dpress2.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(AssistantRegister.this,AssistantLoadActivity.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
AssistantRegister.this.finish();
}
} );
}
}
登陆代码:package cn.com.day01;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class AssistantLoad extends Activity
{
private ImageButton dpress;
private ImageButton dpress2;
private EditText use,password;
Sql db;
SQLiteDatabase sDatabase = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.denglu);
use = (EditText)findViewById(R.id.text1);
password = (EditText)findViewById(R.id.text2);
this.dpress=(ImageButton)findViewById(R.id.Click_Button8);
this.dpress2=(ImageButton)findViewById(R.id.Click_Button9);
db = new Sql(getApplicationContext(), "store.db", null, 1);
sDatabase = db.getWritableDatabase();
dpress.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View v)
{
String i = use.getText().toString();
//String i1 = password.getText().toString();
String userName = "";
String userPw = "";
//编写数据库语句
String select_sql = "select username,password from user_info where username = '"
+ i + "'";
//执行语句
Cursor cursor = sDatabase.rawQuery(select_sql, null);
if(cursor!=null){
cursor.moveToFirst();
try
{
userName = cursor.getString(1);
userPw = cursor.getString(2);
}
catch (Exception e)
{
// TODO: handle exception
userName = "";
userPw = "";
}}
// 判断用户名是否为空
if (i.equals(""))
{
DialogDemo.builder(AssistantLoad.this, "错误信息",
"用户名不能为空!");
}
// 判断密码是否为空
else if (password.getText().toString().equals(""))
{
DialogDemo.builder(AssistantLoad.this, "错误信息",
"密码不能为空!");
}
// 判断用户名和密码是否正确
else if (!(use.getText().toString().equals(userName) && password
.getText().toString().equals(userPw)))
{
DialogDemo.builder(AssistantLoad.this, "错误信息",
"用户名或密码错误,请重新输入");
}
// 全部正确跳转到操作界面
else
{
cursor.close();
Intent intent = new Intent();
//Bundle bundle = new Bundle();
//bundle.putString("username",userName);
//intent.putExtras(bundle);
intent.setClass(AssistantLoad.this, Assistant.class);
startActivity(intent);
Toast.makeText(AssistantLoad.this, "登陆成功", Toast.LENGTH_SHORT).show();
}
}
} );
dpress2.setOnClickListener(new android.view.View.OnClickListener()
{
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(AssistantLoad.this,AssistantLoadActivity.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
AssistantLoad.this.finish();
}
} );
}
}
数据库代码:
package cn.com.day01;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class Sql extends SQLiteOpenHelper{
//创建用户表
String createUserTable = "create table user_info(_id int auto_increment,username char(20),"
+ "password char(20),primary key('_id'));";
// 定义用户表插入语句
String insertStr = "insert into user_info(_id,username,password) values(?,?,?)";
public Sql(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name,factory , version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建用户表,
db.execSQL(createUserTable);
// 插入测试data
/*String[] insertValue = { "0", "admin", "admin" };
db.execSQL(insertStr, insertValue);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
求调试纠错