日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)

瀏覽:28日期:2022-09-23 15:59:17

1、先看一下項目目錄:

Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)

2、新建一個AS項目,創(chuàng)建如上圖所示的目錄結構,然后添加內(nèi)容:

(1)修改添加布局文件:

activity_main.xml:

<?xml version='1.0' encoding='utf-8'?><android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.LoginActivity'><LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <TextView android:layout_width='match_parent' android:layout_height='wrap_content' android:layout_gravity='center' android:gravity='center' android:text='歡迎進入登錄界面!' android:textSize='30dp' android:textStyle='bold' /> <TableLayout android:layout_width='match_parent' android:layout_height='wrap_content' android:stretchColumns='1' > <TableRow> <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='用戶名:' /> <EditTextandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:hint='請輸入用戶名!!!' /> </TableRow> <TableRow> <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='密碼:' /> <EditTextandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:hint='請輸入密碼!!!' /> </TableRow> <TableRow> <TextView /> <LinearLayout><Button android: android:layout_width='100dp' android:layout_height='wrap_content' android:text='登錄' /><Button android: android:layout_width='100dp' android:layout_height='wrap_content' android:text='注冊' /> </LinearLayout> </TableRow> </TableLayout></LinearLayout></android.support.constraint.ConstraintLayout>

activity_register.xml:

<?xml version='1.0' encoding='utf-8'?><android.support.constraint.ConstraintLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.RegisterActivity'><LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical'> <TextView android:layout_width='fill_parent' android:layout_height='wrap_content' android:layout_gravity='center' android:gravity='center' android:text='歡迎進入注冊界面!' android:textSize='30dp' android:textStyle='bold' /> <TableLayout android:layout_width='fill_parent' android:layout_height='wrap_content' android:stretchColumns='1' > <TableRow > <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='用戶名:' /> <EditTextandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:hint='請輸入用戶名!!!' /> </TableRow> <TableRow > <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='密碼:' /> <EditTextandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:hint='請輸入密碼!!!' /> </TableRow> <TableRow > <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='年齡:' /> <EditTextandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:hint='請輸入年齡!!!' /> </TableRow> <TableRow > <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_gravity='center'android:text='性別:'android:textSize='20dp' /> <RadioGroupandroid: android:layout_width='fill_parent'android:layout_height='wrap_content'android:checkedButton='@+id/woman'android:orientation='horizontal' ><RadioButton android: android:text='男' android:layout_height='wrap_content' android:layout_width='wrap_content' /><RadioButton android: android:text='女' android:layout_height='wrap_content' android:layout_width='wrap_content'/> </RadioGroup> </TableRow> <TableRow > <TextView /> <LinearLayout ><Button android: android:layout_width='150dp' android:layout_height='wrap_content' android:text='注冊' /> </LinearLayout> </TableRow> </TableLayout></LinearLayout></android.support.constraint.ConstraintLayout>

(2)在service包DatabaseHelper中添加鏈接AS自帶數(shù)據(jù)庫以及創(chuàng)建表的語句:

package com.example.sqlitelogin.service;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends SQLiteOpenHelper {static String name='user.db';static int dbVersion=1;public DatabaseHelper(Context context) {super(context, name, null, dbVersion);}public void onCreate(SQLiteDatabase db) {String sql='create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),age integer,sex varchar(2))';db.execSQL(sql);}public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}

(3)在service包UserService中用sql語句寫登錄注冊功能的實現(xiàn):

package com.example.sqlitelogin.service;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import com.example.sqlitelogin.User;public class UserService {private DatabaseHelper dbHelper;public UserService(Context context){dbHelper=new DatabaseHelper(context);}public boolean login(String username,String password){SQLiteDatabase sdb=dbHelper.getReadableDatabase();String sql='select * from user where username=? and password=?';Cursor cursor=sdb.rawQuery(sql, new String[]{username,password});if(cursor.moveToFirst()==true){cursor.close();return true;}return false;}public boolean register(User user){SQLiteDatabase sdb=dbHelper.getReadableDatabase();String sql='insert into user(username,password,age,sex) values(?,?,?,?)';Object obj[]={user.getUsername(),user.getPassword(),user.getAge(),user.getSex()};sdb.execSQL(sql, obj);return true;}}

(4)在User文件中聲明要用到的表列名的變量,并對其添加get&&set方法:

package com.example.sqlitelogin;import java.io.Serializable;public class User implements Serializable{ private int id; private String username; private String password; private int age; private String sex; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, int age, String sex) { super(); this.username = username; this.password = password; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return 'User [id=' + id + ', username=' + username + ', password='+ password + ', age=' + age + ', sex=' + sex + ']'; }}

(5)為注冊功能添加activity組件:

package com.example.sqlitelogin;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;import com.example.sqlitelogin.service.UserService;public class RegisterActivity extends AppCompatActivity { EditText username; EditText password; EditText age; RadioGroup sex; Button register; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); findViews(); register.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {String name=username.getText().toString().trim();String pass=password.getText().toString().trim();String agestr=age.getText().toString().trim();String sexstr=((RadioButton)RegisterActivity.this.findViewById(sex.getCheckedRadioButtonId())).getText().toString();Log.i('TAG',name+'_'+pass+'_'+agestr+'_'+sexstr);UserService uService=new UserService(RegisterActivity.this);User user=new User();user.setUsername(name);user.setPassword(pass);user.setAge(Integer.parseInt(agestr));user.setSex(sexstr);uService.register(user);Toast.makeText(RegisterActivity.this, '注冊成功', Toast.LENGTH_LONG).show(); } }); } private void findViews() { username=(EditText) findViewById(R.id.usernameRegister); password=(EditText) findViewById(R.id.passwordRegister); age=(EditText) findViewById(R.id.ageRegister); sex=(RadioGroup) findViewById(R.id.sexRegister); register=(Button) findViewById(R.id.Register); }}

(6)為登錄功能添加activity組件:

package com.example.sqlitelogin;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import com.example.sqlitelogin.service.UserService;public class LoginActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//即activity_login.xml findViews(); } private EditText username; private EditText password; private Button login; private Button register; private void findViews() { username=(EditText) findViewById(R.id.username); password=(EditText) findViewById(R.id.password); login=(Button) findViewById(R.id.login); register=(Button) findViewById(R.id.register); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {String name=username.getText().toString();System.out.println(name);String pass=password.getText().toString();System.out.println(pass);Log.i('TAG',name+'_'+pass);UserService uService=new UserService(LoginActivity.this);boolean flag=uService.login(name, pass);if(flag){ Log.i('TAG','登錄成功'); Toast.makeText(LoginActivity.this, '登錄成功', Toast.LENGTH_LONG).show(); Intent intent = new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent);}else{ Log.i('TAG','登錄失敗'); Toast.makeText(LoginActivity.this, '登錄失敗', Toast.LENGTH_LONG).show();} } }); register.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);startActivity(intent); } }); }}

3、Androidmanifest.xml清單文件中,程序運行必備的內(nèi)容一般都已經(jīng)自動完成添加了。也可以進行修改:

<?xml version='1.0' encoding='utf-8'?><manifest xmlns:android='http://schemas.android.com/apk/res/android' package='com.example.sqlitelogin'> <application android:allowBackup='true' android:icon='@mipmap/ic_launcher' android:label='@string/app_name' android:roundIcon='@mipmap/ic_launcher_round' android:supportsRtl='true' android:theme='@style/AppTheme'> <activity android:name='.RegisterActivity'> <!--<intent-filter>--><!--<action android:name='android.intent.action.MAIN' />--><!--<category android:name='android.intent.category.LAUNCHER' />--> <!--</intent-filter>--> </activity> <activity android:name='.LoginActivity'> <intent-filter><action android:name='android.intent.action.MAIN'/><category android:name='android.intent.category.LAUNCHER'/> </intent-filter> </activity> <!--<uses-library android:name='android.test.runner'/>--> </application></manifest>

4、在模擬器或者真機運行程序,即可!一個連接數(shù)據(jù)庫的登錄注冊功能已經(jīng)實現(xiàn),效果如下:

Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)

補:

如果登錄、注冊的兩個布局文件的 Preview 視圖標紅,將 android.support.constraint.ConstraintLayout 替換為 LinearLayout 即可

源碼下載:

點擊查看

查看創(chuàng)建的數(shù)據(jù)庫以及插入的表數(shù)據(jù):

點擊查看

到此這篇關于Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)的文章就介紹到這了,更多相關Android Studio連接SQLite內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
免费人成精品欧美精品| 麻豆精品在线观看| 久久久久亚洲| 麻豆一区二区在线| 久久一区亚洲| 国产一区二区三区四区| 日本在线观看不卡视频| 久久不射中文字幕| 成人午夜亚洲| 欧美freesex黑人又粗又大| 首页国产欧美久久| 国产高清视频一区二区| 亚洲成人av观看| 伊人国产精品| 欧美激情麻豆| 九九久久婷婷| 日韩精品三级| 日韩欧美自拍| 国产精品日韩| 精品国产一区二| 亚洲+小说+欧美+激情+另类| 国产精品对白| 日韩在线看片| 老牛影视一区二区三区| 精品视频一二| 蜜桃久久av一区| 麻豆理论在线观看| 亚洲精品国产日韩| 精品一区毛片| 久久青青视频| 国产麻豆精品久久| 国产婷婷精品| 香蕉成人av| 免费在线播放第一区高清av| 不卡在线一区二区| 国产成人久久精品一区二区三区| 欧美69视频| 成人在线免费观看91| 婷婷久久免费视频| 9色精品在线| 桃色av一区二区| 精品日本视频| 久久国产乱子精品免费女| 久热综合在线亚洲精品| 欧美~级网站不卡| 日韩精品久久久久久久电影99爱| 欧美另类中文字幕| 亚洲综合中文| 日本大胆欧美人术艺术动态| 欧美一级精品| 久久裸体视频| 亚洲经典在线| 91久久久久| 日日夜夜免费精品| 久久久久网站| av在线日韩| 欧美日韩精品免费观看视完整| 国产免费av国片精品草莓男男| 日欧美一区二区| 91麻豆精品激情在线观看最新| 中文精品视频| 日本不卡视频在线| 国产黄色一区| 日韩一区欧美| 在线一区视频| 性色一区二区| 亚洲日产国产精品| 日本伊人久久| 国产精品videossex久久发布| 国产欧美午夜| 国产videos久久| 99tv成人| 日韩美女精品| 91精品久久久久久久久久不卡| 国产精品日本| 中文字幕亚洲在线观看| 日韩欧美在线中字| 日韩在线播放一区二区| 日韩欧美中文字幕一区二区三区| 久久精品人人| 亚洲精品综合| 九九久久国产| 成人自拍av| 欧美日本不卡高清| 久久蜜桃精品| 欧美精品国产白浆久久久久| 国产一二在线播放| 日韩av电影一区| 欧美日韩高清| 99视频精品全国免费| 综合亚洲色图| 亚洲国产专区校园欧美| 国产精品chinese| 亚洲欧美久久久| 久久精品五月| 亚洲精品裸体| 人在线成免费视频| 亚洲美女91| 亚洲四虎影院| 国产欧美一区二区三区精品观看| 免费av一区| 新版的欧美在线视频| 国产精品毛片视频| 亚洲人成在线影院| 午夜日韩av| 国产国产精品| 日韩在线高清| 亚洲欧美久久久| 久久精品国内一区二区三区| 蜜臀91精品一区二区三区| 日韩在线欧美| 久久精品 人人爱| 97欧美在线视频| 国产精品免费99久久久| 麻豆亚洲精品| 欧美在线亚洲综合一区| 国产精选在线| 国产在视频一区二区三区吞精| 日韩国产精品久久久| 一区在线观看| 国产主播一区| 久久精品免费一区二区三区| 日韩精品看片| 日韩欧美激情电影| 欧美日本久久| 亚洲免费观看高清完整版在线观| 国产精品.xx视频.xxtv| 国产一区清纯| 一区在线免费| 红桃视频亚洲| 欧美日韩国产亚洲一区| 人人精品亚洲| 亚洲天堂成人| 亚洲激情不卡| 手机精品视频在线观看| 亚洲精品人人| 久久精品72免费观看| 国产精品主播| 福利片在线一区二区| 在线人成日本视频| 999国产精品| 只有精品亚洲| 91免费精品国偷自产在线在线| 国产欧美丝祙| 群体交乱之放荡娇妻一区二区| 国产精品二区不卡| 欧美亚洲人成在线| 国产亚洲一区二区手机在线观看| 日韩福利一区| 蜜臀av一区二区在线免费观看| 日本a级不卡| 成人一区不卡| 99国产精品视频免费观看一公开| 在线视频精品| 国产高清精品二区| 五月婷婷六月综合| 欧美日一区二区在线观看| 无码日韩精品一区二区免费| 另类综合日韩欧美亚洲| 美女亚洲一区| 麻豆极品一区二区三区| 午夜免费一区| 老司机精品视频在线播放| 国产精品99免费看| 国产高清亚洲| 三级一区在线视频先锋| 精品一区视频| 青草国产精品| 精品日韩在线| 日韩国产在线一| 在线精品小视频| 青青伊人久久| 精品三级国产| 一区二区三区国产在线| 黑森林国产精品av| 亚洲免费黄色| 国产精品成人一区二区网站软件| 精品国产一级| 亚洲一区成人| 成人片免费看| 一区二区高清| 国内精品福利| 国产精品白丝av嫩草影院| 久久福利精品| 欧美一区二区性| 精品日韩视频| 国产成人久久精品麻豆二区 | 毛片不卡一区二区| 日韩国产精品久久久久久亚洲| 四虎成人精品一区二区免费网站| 欧美精品中文| 亚洲一级高清| 国产精品免费看| 国产在线|日韩| 国内精品伊人| 91综合视频| 丰满少妇一区| 荡女精品导航| 综合日韩av| 精品国产成人|