public class Userlogin extends Activity implementsOnClickListener{privateMySqliteHelper helper;
Button sign;
Button reg;
String name;
String mypwd;privateEditText user;privateEditText pwd;
//http://www.cnblogs.com/xiaobo-Linux/ (小波)赵存档QQ463431476public voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//设置状态栏颜色
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getResources().getColor(R.color.StatusBar));//设置actionbar颜色
ActionBar actionBar =getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0099CC")));
setContentView(R.layout.login);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
user=(EditText)findViewById(R.id.editText1);
pwd=(EditText)findViewById(R.id.editText2);
}public voidinsert()
{
helper= newMySqliteHelper(getApplicationContext());
SQLiteDatabase db=helper.getWritableDatabase();//查询一下,是否用户名重复
String sql1 = "select * from users";
Cursor cursor= db.rawQuery(sql1, null);while(cursor.moveToNext()) {//第一列为id
name = cursor.getString(1); //获取第2列的值,第一列的索引从0开始
mypwd = cursor.getString(2);//获取第3列的值
}if((user.getText().toString().equals(name)))
{
Toast.makeText(this, "已存在此用户,请重新注册", Toast.LENGTH_SHORT).show();
}else{
String sql2= "insert into users(name,pwd) values (‘"+user.getText().toString()+"‘,‘"+pwd.getText().toString()+"‘)";
db.execSQL(sql2);
Toast.makeText(this, "注册成功!", Toast.LENGTH_SHORT).show();
}
}public voidselect()
{
helper= newMySqliteHelper(getApplicationContext());
SQLiteDatabase db=helper.getWritableDatabase();
String sql= "select * from users";
Cursor cursor= db.rawQuery(sql, null);while(cursor.moveToNext()) {//第一列为id
name = cursor.getString(1); //获取第2列的值,第一列的索引从0开始
mypwd = cursor.getString(2);//获取第3列的值
}if((user.getText().toString().equals(name))&&(pwd.getText().toString().equals(mypwd)))
{
Toast.makeText(this, "用户验证成功", Toast.LENGTH_SHORT).show();
Intent MainActivity= newIntent();
MainActivity .setClass(this,MainActivity.class);this.startActivity(MainActivity);
finish();//退出
}else{
Toast.makeText(this, "账号或者密码错误,请重新输入", Toast.LENGTH_SHORT).show();
}
cursor.close();
db.close();//Toast.makeText(this, "已经关闭数据库", Toast.LENGTH_SHORT).show();
}
@Overridepublic voidonClick(View v) {//TODO Auto-generated method stub
switch(v.getId()){caseR.id.button1:
select();break;caseR.id.button2:
insert();break;
}
}