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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

1、先看一下項(xiàng)目目錄:

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

2、新建一個(gè)AS項(xiàng)目,創(chuàng)建如上圖所示的目錄結(jié)構(gòu),然后添加內(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='歡迎進(jìn)入登錄界面!' 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='請(qǐng)輸入用戶名!!!' /> </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='請(qǐng)輸入密碼!!!' /> </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='注冊(cè)' /> </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='歡迎進(jìn)入注冊(cè)界面!' 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='請(qǐng)輸入用戶名!!!' /> </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='請(qǐng)輸入密碼!!!' /> </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='請(qǐng)輸入年齡!!!' /> </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='注冊(cè)' /> </LinearLayout> </TableRow> </TableLayout></LinearLayout></android.support.constraint.ConstraintLayout>

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

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語(yǔ)句寫登錄注冊(cè)功能的實(shí)現(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文件中聲明要用到的表列名的變量,并對(duì)其添加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)為注冊(cè)功能添加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, '注冊(cè)成功', 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清單文件中,程序運(yùn)行必備的內(nèi)容一般都已經(jīng)自動(dòng)完成添加了。也可以進(jìn)行修改:

<?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、在模擬器或者真機(jī)運(yùn)行程序,即可!一個(gè)連接數(shù)據(jù)庫(kù)的登錄注冊(cè)功能已經(jīng)實(shí)現(xiàn),效果如下:

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

補(bǔ):

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

源碼下載:

點(diǎn)擊查看

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

點(diǎn)擊查看

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

標(biāo)簽: Android
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
爽爽淫人综合网网站| 久久婷婷av| 日本成人一区二区| 精品不卡一区| 欧美日韩亚洲一区三区| 天堂av在线| 你懂的国产精品永久在线| 日韩精品一二三区| 亚洲成人国产| 超碰在线99| 欧美aⅴ一区二区三区视频| 国产亚洲一区在线| 精品日韩视频| 一区二区精品伦理...| 欧美在线看片| 日韩中文欧美在线| 亚洲一区中文| 国精品一区二区| 久久精品毛片| 久久中文在线| 麻豆视频一区| 国产亚洲第一伦理第一区| 蜜臀a∨国产成人精品| 自由日本语亚洲人高潮| 精品国产亚洲日本| 久久伊人久久| 精品一区二区三区中文字幕| 国产精品日韩精品中文字幕| 国产欧美啪啪| 国产成人a视频高清在线观看| 国产毛片一区二区三区| 国产精品大片| av免费不卡国产观看| 国产白浆在线免费观看| 日韩在线高清| 国产高清日韩| 久久亚洲精精品中文字幕| 高清一区二区三区av| 久久九九电影| 亚洲一区日韩在线| 日韩激情中文字幕| 国产一区丝袜| 欧美二区视频| 一本一道久久a久久| 美女性感视频久久| 色吊丝一区二区| 亚洲精品影视| 成人在线丰满少妇av| 狠狠爱www人成狠狠爱综合网| 亚洲精品激情| 国产精品久久观看| 日本久久精品| 三级欧美在线一区| 久久免费视频66| 激情欧美日韩一区| 欧美日韩亚洲一区三区| 午夜精品成人av| 国产亚洲人成a在线v网站| 欧美日韩黑人| 欧美日韩91| 性欧美xxxx免费岛国不卡电影| 亚洲激情欧美| 精品成av人一区二区三区| 丝袜美腿一区二区三区| 最新中文字幕在线播放| 亚洲精品日韩久久| 久久av免费| 中文字幕日韩高清在线| 日韩中文影院| 欧美亚洲免费| 亚洲精品午夜av福利久久蜜桃| 亚洲精品自拍| 亚洲一区二区三区久久久| 伊人久久在线| 国产精品成人自拍| 91成人小视频| 免费中文字幕日韩欧美| 欧美日韩水蜜桃| 久久久免费人体| 国产福利资源一区| 婷婷亚洲成人| 国产精品7m凸凹视频分类| 黑森林国产精品av| 国产精品免费99久久久| 欧美日韩午夜电影网| 亚洲毛片在线| 亚洲精品女人| 日本午夜精品一区二区三区电影| 视频一区视频二区中文| 亚洲精品午夜av福利久久蜜桃| 欧美一区二区三区激情视频| 99久久久久久中文字幕一区| 久久久久久免费视频| 日韩欧美一区免费| 日韩欧美二区| 色爱综合网欧美| 麻豆精品在线观看| 日本蜜桃在线观看视频| 欧美黄色网页| 国产精品日韩欧美一区| 日韩高清一区在线| 国产成人精品一区二区三区视频 | 精品一级视频| 国产99精品| 亚洲另类视频| 日韩1区2区| 国产精品日本| 国产日韩欧美高清免费| 亚洲成人不卡| 蜜臀久久久99精品久久久久久| 亚洲精品乱码| 国产成人精品一区二区免费看京| 久久免费黄色| 啪啪亚洲精品| 久久不卡国产精品一区二区| 精品高清久久| 私拍精品福利视频在线一区| 亚洲深夜av| 国产一区二区精品久| 自由日本语亚洲人高潮| 久久精品亚洲| 红桃视频国产精品| 国产精品一级| re久久精品视频| 精品精品99| 精品欧美久久| 国产精选久久| 日本亚洲欧美天堂免费| 亚洲伦乱视频| 里番精品3d一二三区| 蜜桃视频免费观看一区| 久久中文字幕av| 卡一精品卡二卡三网站乱码| 中文字幕免费一区二区| 99成人在线| 99精品在线| 久久久久久久久丰满| 精品99久久| 久久一区欧美| 久久免费精品| 亚洲2区在线| 欧美在线资源| 欧美激情国产在线| 国产精品视频一区二区三区四蜜臂| 亚洲视频www| 欧美日中文字幕| 欧美亚洲日本精品| 久久精品天堂| 国内精品伊人| 精品久久久网| 国产成人精品一区二区三区在线| 国产精品一区亚洲| 国产欧美亚洲一区| 国产欧美在线| 国产日韩亚洲欧美精品| 亚洲精一区二区三区| 免费国产自线拍一欧美视频| 玖玖玖国产精品| 麻豆mv在线观看| 美女精品视频在线| 动漫av一区| 欧美国产一级| 国产综合精品一区| 一区二区三区四区日韩| 九九在线精品| 日本aⅴ精品一区二区三区| 日韩三区四区| 97精品一区二区| 日本在线精品| 亚洲一区二区免费在线观看| 欧美精品一二| 国产精品自拍区| 国产一区二区三区亚洲综合| 精品欧美久久| 久久男人av| 久久av一区二区三区| 老色鬼精品视频在线观看播放| 亚洲高清二区| 欧美日韩亚洲三区| 精品一区亚洲| 国产欧美欧美| 日韩网站在线| 国产在线观看91一区二区三区| 亚洲欧美日韩精品一区二区 | 麻豆精品在线播放| 国产毛片一区| 久久精品二区三区| 精品伊人久久| 日韩精彩视频在线观看| 91久久中文| 日韩精品欧美| 国产成人精品一区二区免费看京| 免费看欧美美女黄的网站| 激情婷婷久久| а√天堂8资源在线| 午夜电影一区| jiujiure精品视频播放| 鲁鲁在线中文| 日韩不卡一区| 久久精品国产免费|