正常一个安卓应用必不可少的功能就是登录与注册了,今天我们说说android中登录&注册。
先理一下简单流程:
(一)SQLiteOpenHelper,进入APP后建库建表。
注册功能实现:
注册功能非空校验,数据库添加数据,页面跳转的实现。
登录功能实现:
登录功能非空校验,数据库查询数据,页面跳转的实现。
知识点:学习SQLite数据库知识。(可以参考第一行代码)
下面结合源代码看一步步如何实现:
自己编写类继承SQLiteOpenHelper,在里面写相应的建表语句,如:
- public static final String CREATE_USER = "create table User ("
- + "id integer primary key , "
- + "name text, "
- + "password text)";
重写onCreate(第一次建库时调用)、onUpgrade(升级时调用)方法。
在主类中生成自己刚才编写类的对象并初始化,建立数据库,如:
dbHelper = new MyDatabaseHelper(this, "Meeting.db", null, 1);
至此,完成建库建表操作。
非空校验又是如何实现?这里借助TextUtils类的isEmpty方法,方便快捷,如:
- String userName = userName_et_main.getText().toString();
- if(TextUtils.isEmpty(userName)){
- Toast.makeText(getApplication(), "用户名不能为空!", Toast.LENGTH_SHORT).show();
- }
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- Cursor cursor = db.query("User", null, null, null, null, null,
- null);
- if (cursor.moveToFirst()) {
- do {
- String nameCur = cursor.getString(cursor
- .getColumnIndex("name"));
- String passwordCur = cursor.getString(cursor
- .getColumnIndex("password"));
- int idCur = cursor.getInt(cursor
- .getColumnIndex("id"));
- Log.d("ws:", "nameCur-" + nameCur);
- Log.d("ws:", "passwordCur-" + passwordCur);
- Log.d("ws:", "idCur-" + idCur);
- if(userName.equals(nameCur)&&passWord.equals(passwordCur)){
- Intent intent = new Intent();
- intent.setClass(MainActivity.this, ListActivity.class);
- startActivity(intent);
- break;
- }
- } while (cursor.moveToNext());
- }
- cursor.close();
这样,一个使用SQLite的登录功能就成功了。
注册功能类似,不同之处是添加数据使用了insert方法和ContentValues类,直接贴源代码,就不详细描述了。
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import dbutils.MyDatabaseHelper;
-
- public class RegistActivity extends Activity{
- private Button regist_btn_regist;
- private EditText userName_et_regist;
- private EditText passWord_et_regist;
- private EditText id_et_regist;
- private MyDatabaseHelper dbHelper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.regist);
- dbHelper = new MyDatabaseHelper(this, "Meeting.db", null, 1);
- initView();
- regist_btn_regist.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String userName = userName_et_regist.getText().toString();
- String passWord = passWord_et_regist.getText().toString();
- String id = id_et_regist.getText().toString();
- if(TextUtils.isEmpty(userName)){
- Toast.makeText(getApplication(), "用户名不能为空!", Toast.LENGTH_SHORT).show();
- }
- if(TextUtils.isEmpty(passWord)){
- Toast.makeText(getApplication(), "密码不能为空!", Toast.LENGTH_SHORT).show();
- }
- if(TextUtils.isEmpty(id)){
- Toast.makeText(getApplication(), "工号不能为空!", Toast.LENGTH_SHORT).show();
- }
- if(!TextUtils.isEmpty(userName)&&!TextUtils.isEmpty(passWord)&&!TextUtils.isEmpty(id)){
- Log.d("ws-userName:",userName);
- Log.d("ws-passWord:",passWord);
- Log.d("ws-id:",id);
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("name", userName);
- values.put("password", passWord);
- values.put("id", id);
- db.insert("User", null, values);
- Log.d("ws:","db.insert succeed.");
- values.clear();
- Intent intent = new Intent();
- intent.setClass(RegistActivity.this, ListActivity.class);
- startActivity(intent);
- }
- }
- });
- }
- private void initView() {
- userName_et_regist = (EditText) findViewById(R.id.userName_et_regist);
- passWord_et_regist = (EditText) findViewById(R.id.passWord_et_regist);
- id_et_regist = (EditText) findViewById(R.id.id_et_regist);
- regist_btn_regist = (Button) findViewById(R.id.regist_btn_regist);
- }
- }