Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能
本文實(shí)例為大家分享了Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能的具體代碼,供大家參考,具體內(nèi)容如下
布局文件
border.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 布局的背景顏色--> <!-- <solid android:color="#FFFFFF" />--> <!-- 邊框線的粗細(xì)和顏色--> <stroke android:width="0.01dp" android:color="#000" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="5dp" /> <!-- 圓角--> <corners android:radius="5dp" /> </shape>
<?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=".MainActivity"> <LinearLayout android:padding="5dp" android:background="@drawable/border" android:orientation="vertical" android:layout_gravity="center_horizontal" android:layout_width="360dp" android:layout_height="112dp"> <LinearLayout android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:layout_marginRight="15dp" android:layout_gravity="center_vertical" android:layout_width="30dp" android:layout_height="30dp" app:srcCompat="@drawable/usn" android:id="@+id/usn"/> <!-- android:background="@null" 去掉下劃線 --> <EditText android:singleLine="true" android:background="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="text" android:hint="用戶名" android:ems="10" android:id="@+id/username"/> </LinearLayout> <!-- 水平線--> <View android:layout_height="0.5dip" android:background="#686868" android:layout_width="match_parent"/> <LinearLayout android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="50dp"> <ImageView android:layout_marginRight="15dp" android:layout_gravity="center_vertical" android:layout_width="30dp" android:layout_height="30dp" app:srcCompat="@drawable/pwd" android:id="@+id/密碼"/> <EditText android:singleLine="true" android:background="@null" android:layout_width="match_parent" android:layout_height="match_parent" android:inputType="textPassword" android:hint="密碼" android:ems="10" android:id="@+id/password"/> </LinearLayout> </LinearLayout> <Button android:layout_gravity="center_horizontal" android:background="#EF8D89" android:layout_marginTop="20dp" android:text="登 錄" android:onClick="userLogin" android:layout_width="360dp" android:layout_height="wrap_content" android:id="@+id/login"/> </android.support.constraint.ConstraintLayout>
MainActivity類
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //訪問(wèn)數(shù)據(jù)庫(kù)的類 SQLiteDatabase db; //定義常量,作為消息的key public final static String MESSAGE_KEY="com.android2"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * (參數(shù))1、context MainActivity * 2、name 數(shù)據(jù)庫(kù)名 * 3、 * 4、版本號(hào) */ final DatabaseHelper databaseHelper = new DatabaseHelper(this,"emis.db",null,2); //獲得讀取數(shù)據(jù)庫(kù)權(quán)限 db = databaseHelper.getReadableDatabase(); setContentView(R.layout.activity_main); } /*響應(yīng)*/ private void userLogin() { EditText et1 = findViewById(R.id.username); String username = et1.getText().toString(); EditText et2 = findViewById(R.id.password); String password = et2.getText().toString(); //游標(biāo)類Cursor 負(fù)責(zé)生成讀寫數(shù)據(jù)庫(kù)的對(duì)象 Cursor cursor = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?",new String[]{username,password}); //數(shù)據(jù)庫(kù)中有此數(shù)據(jù),登錄成功 if(cursor.getCount()>0){ Intent intent = new Intent(this,ReceiveActivity.class); intent.putExtra(MESSAGE_KEY,username); startActivity(intent); } else{ Toast.makeText(MainActivity.this,"用戶名或密碼錯(cuò)誤!",Toast.LENGTH_SHORT).show(); } } }
ReceiveActivity類及布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ReceiveActivity" > <TextView android:textSize="24dp" android:layout_gravity="center_vertical" android:id="@+id/output" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
package com.android02; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class ReceiveActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_receive); //獲取intent引用 Intent intent = getIntent(); //以MESSAGE_KEY獲取獲取編輯框文字 String message = intent.getStringExtra(MainActivity.MESSAGE_KEY); //以id獲取TextView TextView textView = findViewById(R.id.output); //顯示message textView.setText("歡迎!"+message); } }
測(cè)試:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SQLite基本用法詳解
- android中SQLite使用及特點(diǎn)
- Android使用gradle讀取并保存數(shù)據(jù)到BuildConfg流程詳解
- Android通過(guò)ViewModel保存數(shù)據(jù)實(shí)現(xiàn)多頁(yè)面的數(shù)據(jù)共享功能
- Android學(xué)習(xí)筆記-保存數(shù)據(jù)到SQL數(shù)據(jù)庫(kù)中(Saving Data in SQL Databases)
- Android 實(shí)現(xiàn)永久保存數(shù)據(jù)的方法詳解
- Android 如何使用SQLite保存數(shù)據(jù)
相關(guān)文章
Android游戲開發(fā)實(shí)踐之人物移動(dòng)地圖的平滑滾動(dòng)處理
玩過(guò)rpg游戲的朋友應(yīng)該都知道RPG的游戲地圖一般都比較大 今天我和大家分享一下在RPG游戲中如何來(lái)處理超出手機(jī)屏幕大小的游戲地圖。2014-06-06Android創(chuàng)建與解析XML(二)——詳解Dom方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Dom方式 ,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11Android實(shí)現(xiàn)后臺(tái)開啟服務(wù)默默拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)后臺(tái)開啟服務(wù)默默拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Ionic2創(chuàng)建App啟動(dòng)頁(yè)左右滑動(dòng)歡迎界面
使用Ionic2創(chuàng)建應(yīng)用非常簡(jiǎn)單,只需在V1的命令后跟上--v2即可.這篇文章主要介紹了Ionic2創(chuàng)建App啟動(dòng)頁(yè)左右滑動(dòng)歡迎界面的相關(guān)資料,需要的朋友可以參考下2016-10-10Android給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖
這篇文章主要為大家詳細(xì)介紹了Android給scrollView截圖超過(guò)屏幕大小形成長(zhǎng)圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Flutter實(shí)現(xiàn)自定義篩選框的示例代碼
本文主要介紹了Flutter實(shí)現(xiàn)自定義篩選框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07