Android SQLite数据库连接实现登录功能
作者:Red&&Black 发布时间:2022-09-04 01:51:46
标签:Android,SQLite,登录
本文实例为大家分享了Android SQLite数据库连接实现登录功能的具体代码,供大家参考,具体内容如下
布局文件
border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 布局的背景颜色-->
<!-- <solid android:color="#FFFFFF" />-->
<!-- 边框线的粗细和颜色-->
<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{
//访问数据库的类
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类及布局
<?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);
}
}
测试:
来源:https://blog.csdn.net/m0_46267375/article/details/109003433


猜你喜欢
- 把spring-boot项目按照平常的web项目一样发布到tomcat容器下一、修改打包形式在pom.xml里设置 <packagin
- 结构化查询语言(SQL)是一种标准化的语言,它允许你在数据库上执行操作,如创建项目,读取内容,内容更新和删除条目。SQL是所有可能会使用几乎
- 你是否遇到过,出现异常的时候也需要给一个默认值,让程序可以继续运行下去?一般的做法就是 一个达到try catch,然后在finally里面
- 下面一段代码给大家介绍了android 自定义顶部导航栏控件功能,具体代码如下所示:class HeaderBar @JvmOverload
- Guava EventBusEventBus是Guava的事件处理机制,是设计模式中观察者模式(生产/消费者编程模型)的优雅实现。对于事件监
- 其实说实话,没有多大的可比较性,它们是完全不同的两个东西,它们的抽象不在同一个层级上。但是为了让大家更好的理解,还是做一个比较吧,毕竟它们都
- 前面在学习鸿洋大神的一些自定义的View文章,看到了自定义ViewGroup实现浮动标签,初步看了下他的思路以及结合自己的思路完成了自己的浮
- 旋转扭曲特效是指在一个圆形区域内扭曲所渲染的图像,其他像素的旋转程度随着距离的变化而变化。具体可以通过修改Shader来实现。原始图片扭曲图
- 之前在做分页时,很多朋友都是用Jquery分页插件,之前我就用的jquery.paper,有需要的朋友可以联系我,接下来小编给大家分享用Jq
- GlobalLock的作用对于某条数据进行更新操作,如果全局事务正在进行,当某个本地事务需要更新该数据时,需要使用@GlobalLock确保
- 前言由于不小心将and或者or写在了语句后面,导致mybatis无法自主判别,这种问题在新上手的同学中很是常见。下面我们探讨一下,在哪些情况
- 本文实例为大家分享了Spring实现默认标签解析流程的具体代码,供大家参考,具体内容如下承接上文,进入parseBeanDefinition
- 在协程启动模式中已经知道async是可以返回结果的,但是只返回一个,那么在复杂场景下就会不够用了,所以Channel就出现了。1.认识Cha
- 本文实例为大家分享了maven插件安装教程,供大家参考,具体内容如下如果能打印如上信息,说明到此 Maven已经在你的电脑上安装完成。mvn
- 本文实例讲述了Android使用WebView.loadUri()打开网页的方法。分享给大家供大家参考,具体如下:程序如下所示:import
- ==,equals()与hashcode()"=="在讲解之前,我们是怎么接触到==的,我们在作比较时经常用到 ==,
- Thread parameterThread_t = null; private void Print_DetailForm_S
- 在日常开发的过程中我们经常会需要调用第三方组件或者数据库,有的时候可能会因为网络抖动或者下游服务抖动,导致我们某次查询失败。这种时候我们往往
- 一、简介SpringBoot 中给普通变量注入值只需在变量上添加 @Value 注解即可。application.properties 配置
- 今天我们介绍的是jpa删除和事务的一些坑,接下来看看具体内容。业务场景(这是一个在线考试系统)和代码:根据问题的id删除答案reposito