news 2026/8/16 10:18:54

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进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/14 7:15:06

别再把数据管道当“体力活”了:从单体任务到事件驱动的升级之路

别再把数据管道当“体力活”了&#xff1a;从单体任务到事件驱动的升级之路 作者&#xff1a;Echo_Wish兄弟们&#xff0c;咱们今天聊点“掏心窝子”的大数据经验&#xff1a;现代数据管道到底应该怎么设计&#xff1f; 很多公司到现在还在用“单体式任务管道”——Airflow 一堆…

作者头像 李华
网站建设 2026/8/15 13:08:44

【面试现场】谢飞机大战Java面试官:从基础到架构的爆笑面试实录

【面试现场】谢飞机大战Java面试官&#xff1a;从基础到架构的爆笑面试实录 第一轮&#xff1a;Java基础与集合框架 面试官&#xff1a;谢飞机同学&#xff0c;你好。我是今天的面试官&#xff0c;我们开始第一轮技术面试。首先问个基础问题&#xff1a;Java中ArrayList和Linke…

作者头像 李华
网站建设 2026/8/15 17:44:07

2025.12.11总结

工作日精进&#xff1a;明天版本过点&#xff0c;今晚每个人都搞得很晚&#xff0c;我也是晚上11&#xff1a;30才回到租房。每天就这么起床&#xff0c;上班&#xff0c;吃饭&#xff0c;下班。租房&#xff0c;工位&#xff0c;食堂&#xff0c;三点一线。下班后&#xff0c;…

作者头像 李华
网站建设 2026/8/15 17:44:02

C++提高编程学习(更新中....)

1 模板1.1 模板的概念模板是一种通用的编程工具&#xff0c;允许使用虚拟类型&#xff08;泛型&#xff09;定义函数或类&#xff0c;从而实现对不同数据类型的复用。通过模板编写的代码可以自动适应多种数据类型&#xff0c;减少重复代码。模板的特点&#xff1a;通用性&#…

作者头像 李华
网站建设 2026/8/15 12:42:26

编程范式悄然转舵:从“规则编织”到“模型生长”​

我们正站在编程史的拐点——从“基于规则编程”的精密齿轮时代&#xff0c;迈入“基于基础模型编程”的智能涌现纪元。过去&#xff0c;我们用代码刻写每一条逻辑边界&#xff0c;像搭积木般构建功能&#xff1b;如今&#xff0c;大模型成了“会学习的土壤”&#xff0c;高价值…

作者头像 李华