news 2025/12/31 10:28:09

Android数据库MVC模式应用——数据查询(用户登陆)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android数据库MVC模式应用——数据查询(用户登陆)

1.Model层——User类的设计

同上一篇文章用户添加。

2. dao层——UserDao的设计

在UserDao中添加登陆方法的代码。

public boolean Login(User user){ //根据用户信息执行查询操作,查到返回true,没查到返回false String strUserName=user.getUsername(); String strPassword=user.getPassword(); Integer type = user.getType(); Cursor cursor=db.query("user_table",null,"username=? and password=? and type=?", new String[]{strUserName,strPassword,String.valueOf(type)},null,null,null ); if(cursor.moveToNext()){ return true; }else { return false; } }

3. service层——UserService设计

(1)接口UserService添加登陆方法声明。

public boolean Login(User user);

(2)实现类UserServiceImpl添加登陆方法的实现。

public boolean Login(User user) { return userDao.Login(user); }

4. View层——LoginActivity设计

(1)布局文件activity_login.xml设计

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:padding="12dp" android:gravity="center" android:background="@color/colorBlue" tools:context=".LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="450dp" android:background="@drawable/login_box_background" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="40sp" android:layout_marginBottom="20dp" android:gravity="center" android:text="Welcome" /> <EditText android:id="@+id/etUserName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:hint="用户名" android:textSize="@dimen/fontSize" android:drawableLeft="@drawable/username" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" android:hint="密码" android:textSize="@dimen/fontSize" android:drawableLeft="@drawable/password" android:maxLength="10" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/radStudent" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:checked="true" android:textSize="@dimen/fontSize" android:text="学生" /> <RadioButton android:id="@+id/radTeacher" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textSize="@dimen/fontSize" android:text="教师" /> </RadioGroup> </LinearLayout> <Button android:id="@+id/btLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="@drawable/new_btn_background" android:textColor="@color/colorWhite" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:textSize="@dimen/fontSize" android:text="Login" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <CheckBox android:id="@+id/chkAutoLogin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="@dimen/fontSize" android:text="自动登陆" /> <CheckBox android:id="@+id/chkSavePass" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:checked="true" android:text="记住密码" android:textSize="@dimen/fontSize" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal"> <TextView android:id="@+id/tvReg" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:textSize="@dimen/fontSize" android:textColor="@color/colorBlue" android:layout_marginLeft="30dp" android:text="用户注册" android:textStyle="bold" /> <TextView android:id="@+id/tvForgetPass" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:textSize="@dimen/fontSize" android:textColor="@color/colorBlue" android:layout_marginLeft="50dp" android:textStyle="bold" android:text="忘记密码" /> </LinearLayout> </LinearLayout> </LinearLayout>

(2)主类LoginActivity设计

package com.example.myactivitydemo; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import com.example.myactivitydemo.entity.User; import com.example.myactivitydemo.service.UserService; import com.example.myactivitydemo.service.impl.UserServiceImpl; import com.example.myactivitydemo.util.DbHelper; import com.example.myactivitydemo.util.MD5Util; public class LoginActivity extends AppCompatActivity { private EditText etUserName; private EditText etPassword; private Button btLogin; private CheckBox chkSavePass; private CheckBox chkAutoLogin; private TextView tvReg; private RadioButton radStudent; private RadioButton radTeacher; //定义两个成员变量 private SharedPreferences sp; private SharedPreferences.Editor editor; private UserService userService; private int type; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); //实例化 sp=getSharedPreferences("user",0); editor=sp.edit(); //读取用户信息 String strUserName=sp.getString("username",""); String strPassword=sp.getString("password",""); etUserName.setText(strUserName); etPassword.setText(strPassword); tvReg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent); } }); btLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String strUserName=etUserName.getText().toString().trim(); String strPassword=etPassword.getText().toString().trim(); if(radStudent.isChecked()){ type=1; }else{ type=2; } //身份验证 userService=new UserServiceImpl(LoginActivity.this); User user=new User(); user.setUsername(strUserName); user.setPassword(strPassword); user.setType(type); if(userService.Login(user)){ if(chkSavePass.isChecked()){ //执行记住密码的操作 editor.putString("username",strUserName); editor.putString("password",etPassword.getText().toString()); editor.commit();//提交 } Intent intent=new Intent(LoginActivity.this,InfoActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this, "用户名或密码错误!",Toast.LENGTH_LONG).show(); } } }); } public void initView(){ etUserName=findViewById(R.id.etUserName); etPassword=findViewById(R.id.etPassword); btLogin=findViewById(R.id.btLogin); chkAutoLogin=findViewById(R.id.chkAutoLogin); chkSavePass=findViewById(R.id.chkSavePass); tvReg=findViewById(R.id.tvReg); radStudent=findViewById(R.id.radStudent); radTeacher=findViewById(R.id.radTeacher); } }

最后是实现效果:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2025/12/23 2:03:55

Ascend C Tiling维度切分策略全解 - Block、Core与硬件单元的映射艺术

目录 &#x1f4d6; 摘要 &#x1f3d7;️ Tiling架构设计 1.1 两种开发模式的深度对比 1.2 Tiling基本概念的硬件映射 ⚙️ 核心算法实现 2.1 基础Tiling实现流程 2.2 非对齐shape处理算法 &#x1f680; 实战&#xff1a;企业级Tiling框架 3.1 完整实现示例 3.2 常…

作者头像 李华
网站建设 2025/12/27 2:25:24

Netbank与Thredd合作,助力其在菲律宾全境推出新一代卡片即服务解决方案

菲律宾首个完全受监管的嵌入式银行业务平台旨在通过其全新的卡片即服务&#xff08;CaaS&#xff09;解决方案&#xff0c;助力该国银行和金融科技公司快速推出并扩展现代卡片服务 领先的下一代全球支付处理商Thredd今日宣布&#xff0c;菲律宾首家完全受监管的嵌入式银行业务平…

作者头像 李华
网站建设 2025/12/24 1:11:03

铁轨缺陷检测数据集介绍及使用说明

铁轨缺陷检测数据集 类别为damage,dirt,unknown,gap,d_dent,d_crush,d_scratch,d_slant 原数据集399张 扩充三倍后一共1596张 txt或xml都可 yolov5s训练出的结果文件和权重一、数据集概述本数据集主要用于铁轨缺陷的检测与识别&#xff0c;包含了多种类别的铁轨图像。原始数据集…

作者头像 李华
网站建设 2025/12/24 2:29:19

毕设开源 大数据共享单车数据分析与可视化(源码分享)

文章目录 0 前言1 课题背景2 数据清洗3 数据可视化热力图整体特征分布**查看2011-2012间的单车租借情况**天气对于租借数量的影响湿度与温度对于租借数量的影响注册用户与未注册用户 4 总结&#xff1a;5 最后 0 前言 &#x1f525;这两年开始毕业设计和毕业答辩的要求和难度不…

作者头像 李华
网站建设 2025/12/22 18:24:39

OJ刷题小结

1.1题目1.2代码#include <stdio.h> #include <string.h>int add(int a, int b) {return a b; }int main() {int n;scanf("%d", &n);getchar();char input[20]; for (int i 0; i < n; i) {fgets(input, sizeof(input), stdin);int a, b;sscanf(i…

作者头像 李华