当前位置: 首页 > news >正文

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

最后是实现效果:

http://www.zskr.cn/news/84161.html

相关文章:

  • XUnity.AutoTranslator游戏翻译工具:5分钟实现游戏文本实时翻译的完整教程
  • Netbank与Thredd合作,助力其在菲律宾全境推出新一代卡片即服务解决方案
  • 【企业级Agent安全配置】:Docker环境下99%的人都忽略的5大安全隐患
  • 终极指南:深度解析Intel CPU电压调节的完整技术方案
  • 京东健康联合京东金榜发布2025年度三大品类金榜
  • BepInEx框架实战指南:从入门到精通的Unity模组开发全解析
  • 告别模糊卡顿!Wan2.2-T2V-A14B实现高分辨率视频流畅生成
  • Windows右键菜单大扫除:从杂乱无章到高效简洁的完整改造方案
  • 德意志飞机莱比锡总装线封顶庆典圆满举行 加速D328eco产业化进程
  • Lonsdor K518 Pro FCV Volvo LYNK CO License Activation – Key Programming for Mechanics Car Owners
  • 算法题 数据流中的第 K 大元素
  • 互聯網幻覺
  • OpenHarmony Flutter 分布式设备发现与组网:跨设备无感连接与动态组网方案
  • 解决力扣第26题,论删除重复项
  • vivo端侧AI新突破:30亿参数模型实现GUI界面深度理解,多模态能力领跑行业
  • 人工智能深度学习实战:手写数字识别指南
  • ISO图接点显示分区号
  • Hadoop-动态刷新hdfs/yarn配置
  • BetterGI深度评测:原神自动化工具的效率革命实战体验
  • Bili2text:重新定义视频内容处理效率
  • MoE架构加持的Wan2.2-T2V-A14B,如何提升动态细节表现力?
  • 揭秘空间转录组数据分析:如何用R语言完成单细胞分辨率下的精准定位
  • 从C++/MFC到CEF与TypeScript的桌面架构演进
  • 基于CANoe的CAPL语言打造UDS Bootloader刷写上位机程序
  • 【OD刷题笔记】- 分糖果
  • 编程范式悄然转舵:从“规则编织”到“模型生长”​
  • 【R Shiny多模态可视化实战】:掌握高效整合文本、图像与数据的三大核心技巧
  • DPJ-126 基于STC89C52的酒驾检测系统设计(源代码+proteus仿真)
  • Blender 3MF插件实战指南:从安装到精通
  • 你还在手动排查量子代码?VSCode Azure QDK自动调试方案曝光