android簡單登陸和注冊功能實現+SQLite數據庫學習


最近初學android,做了實驗室老師給的基本任務,就是簡單的登陸和注冊,並能通過SQLite實現登陸,SQlLite是嵌入在安卓設備中的

好了下面是主要代碼:

數據庫的建立:

這里我只是建立了一個用簡單的存儲用戶名和密碼的表單

MyDBHelper.java

public class MyDBHelper extends SQLiteOpenHelper {
    
    public static final String CREATE_USERDATA="create table userData(" +
            "id integer primary key autoincrement,"
            +"nametext,"
            +"password text)";

    private Context mContext;

    public MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory cursorFactory,int version){
        super(context,name,cursorFactory,version);
        mContext=context;
    }

    public  void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_USERDATA);
    }

    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
        //onCreate(db);
    }

}

注冊:

Main2Activity.java
 
         

public class Main2Activity extends AppCompatActivity {

private MyDBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

dbHelper = new MyDBHelper(this,"UserStore.db",null,1);
}

public void logon(View view){
//SQLiteDatabase db=dbHelper.getWritableDatabase();

EditText editText3=(EditText)findViewById(R.id.editText3);
EditText editText4=(EditText)findViewById(R.id.editText4);
String newname =editText3.getText().toString();
String password=editText4.getText().toString();
if (CheckIsDataAlreadyInDBorNot(newname)) {
Toast.makeText(this,"該用戶名已被注冊,注冊失敗",Toast.LENGTH_SHORT).show();
}
else {

if (register(newname, password)) {
Toast.makeText(this, "插入數據表成功", Toast.LENGTH_SHORT).show();
}
}
}
//向數據庫插入數據
public boolean register(String username,String password){
SQLiteDatabase db= dbHelper.getWritableDatabase();
/*String sql = "insert into userData(name,password) value(?,?)";
Object obj[]={username,password};
db.execSQL(sql,obj);*/
ContentValues values=new ContentValues();
values.put("name",username);
values.put("password",password);
db.insert("userData",null,values);
db.close();
//db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
return true;
}
//檢驗用戶名是否已存在
public boolean CheckIsDataAlreadyInDBorNot(String value){
SQLiteDatabase db=dbHelper.getWritableDatabase();
String Query = "Select * from userData where name =?";
Cursor cursor = db.rawQuery(Query,new String[] { value });
if (cursor.getCount()>0){
cursor.close();
return true;
}
cursor.close();
return false;
}

}


登陸:

MainActivity.java
public class MainActivity extends AppCompatActivity {
    private MyDBHelper dbHelper;
    private EditText username;
    private EditText userpassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        dbHelper = new MyDBHelper(this,"UserStore.db",null,1);
    }
    
     //點擊注冊按鈕進入注冊頁面
    public void logonClicked(View view){
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
    }


    //點擊登錄按鈕
    public void loginClicked(View view) {
        username=(EditText)findViewById(R.id.editText2);
        userpassword=(EditText)findViewById(R.id.editText);
        String userName=username.getText().toString();
        String passWord=userpassword.getText().toString();
        if (login(userName,passWord)) {
            Toast.makeText(MainActivity.this, "登陸成功(ZY,111)", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(MainActivity.this, "登陸失敗", Toast.LENGTH_SHORT).show();
        }
    }

    //驗證登錄
    public boolean login(String username,String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sql = "select * from userData where name=? and password=?";
        Cursor cursor = db.rawQuery(sql, new String[] {username, password});
        if (cursor.moveToFirst()) {
            cursor.close();
            return true;
        }
        return false;
    }

好了,就醬。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM