Android SQLite数据库连接实现登录功能
时间:2021-08-28 09:28:13|栏目:Android代码|点击: 次
本文实例为大家分享了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); } }
测试:
上一篇:android中使用SharedPreferences进行数据存储的操作方法
栏 目:Android代码
本文标题:Android SQLite数据库连接实现登录功能
本文地址:http://www.codeinn.net/misctech/170553.html