当前位置: 首页 > 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.jsqmd.com/news/73634/

相关文章:

  • Easily Program Borgward Keys: Lonsdor K518 PRO FCV License Activation
  • XUnity.AutoTranslator游戏翻译工具:5分钟实现游戏文本实时翻译的完整教程
  • Wan2.2-T2V-A14B支持长时间序列生成吗?实测60秒连续输出
  • 【R语言高手进阶指南】:5步搞定农业产量的复杂数据建模
  • 关于文章仿写的专业指南与实践要点
  • 【高效运维必看】:Agent服务在Docker中跨环境迁移的7种优化方案
  • Netbank与Thredd合作,助力其在菲律宾全境推出新一代卡片即服务解决方案
  • 【企业级Agent安全配置】:Docker环境下99%的人都忽略的5大安全隐患
  • 闲鱼自动化终极指南:3个技巧让你告别重复劳动
  • 如何在 LTspice放置 .op data 并能够设置显示的小数点个数?
  • UI+Widget:鸿蒙/Flutter等声明式UI框架的核心设计范式深度解析
  • VSCode量子编程必备技能:构建高效监控面板的5个关键步骤(专家级实战指南)
  • 【面试现场】谢飞机大战Java面试官:从基础到架构的爆笑面试实录
  • 机器学习算法二:逻辑回归
  • JavaEE进阶——MyBatis动态SQL与图书管理系统实战
  • 毕设开源 大数据共享单车数据分析与可视化(源码分享)
  • Windows右键菜单终极优化:ContextMenuManager完整使用指南
  • Solon AI MCP v3.7.3, v3.6.6 发布
  • MySQL进阶篇——存储结构,索引
  • 百度ERNIE-4.5-VL-28B-A3B-Base震撼发布:多模态大模型基座开启智能新纪元
  • RN性能优化实战:从卡顿到丝滑的进阶之路
  • 鸿蒙智慧屏与Flutter适配:无硬件功能的兼容处理
  • Codeforces Round 1070 (Div. 2)
  • DownKyi高效下载指南:从入门到精通
  • 终极指南:深度解析Intel CPU电压调节的完整技术方案
  • 深度指南:如何设计Prompt引导DeepSeek生成高效的分步故障排查流程
  • 京东健康联合京东金榜发布2025年度三大品类金榜
  • 3分钟掌握B站视频下载:哔哩下载姬终极使用指南
  • 学习总结
  • BepInEx框架实战指南:从入门到精通的Unity模组开发全解析