安卓连接mysql登陆_Android SQLite数据库连接实现登录功能

本文实例为大家分享了Android SQLite数据库连接实现登录功能的具体代码,供大家参考,具体内容如下

布局文件

border.xml

android:width="0.01dp"

android:color="#000" />

android:bottom="5dp"

android:left="5dp"

android:right="5dp"

android:top="5dp" />

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">

android:padding="5dp"

android:background="@drawable/border"

android:orientation="vertical"

android:layout_gravity="center_horizontal"

android:layout_width="360dp"

android:layout_height="112dp">

android:orientation="horizontal"

android:layout_gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="50dp">

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: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"/>

android:layout_height="0.5dip"

android:background="#686868"

android:layout_width="match_parent"/>

android:orientation="horizontal"

android:layout_gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="50dp">

android:layout_marginRight="15dp"

android:layout_gravity="center_vertical"

android:layout_width="30dp"

android:layout_height="30dp" app:srcCompat="@drawable/pwd" android:id="@+id/密码"/>

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"/>

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"/>

MainActivity类

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

//访问数据库的类

SQLiteDatabase db;

//定义常量,作为消息的key

public final static String MESSAGE_KEY="com.android2";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

/**

* (参数)1、context MainActivity

* 2、name 数据库名

* 3、

* 4、版本号

*/

final DatabaseHelper databaseHelper = new DatabaseHelper(this,"emis.db",null,2);

//获得读取数据库权限

db = databaseHelper.getReadableDatabase();

setContentView(R.layout.activity_main);

}

/*响应*/

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();

//游标类Cursor 负责生成读写数据库的对象

Cursor cursor = db.rawQuery("SELECT * FROM users WHERE username=? AND password=?",new String[]{username,password});

//数据库中有此数据,登录成功

if(cursor.getCount()>0){

Intent intent = new Intent(this,ReceiveActivity.class);

intent.putExtra(MESSAGE_KEY,username);

startActivity(intent);

}

else{

Toast.makeText(MainActivity.this,"用户名或密码错误!",Toast.LENGTH_SHORT).show();

}

}

}

ReceiveActivity类及布局

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"

>

android:textSize="24dp"

android:layout_gravity="center_vertical"

android:id="@+id/output"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

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);

}

}

测试:

cb138d25298c6d91a654923fef5291f9.png

03e0575141e5aa4d5e4d7461f45b1bb6.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值