news 2026/2/2 22:15:15

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/1/25 2:01:57

Skyhigh Security升级数据安全态势管理(DSPM)能力,助力企业满足《数字个人数据保护法》(DPDPA)合规要求,强化亚太地区数据保护

功能升级方案应对复杂隐私与监管需求 数据优先型安全服务边缘(SSE)领域的领导者Skyhigh Security今日宣布&#xff0c;在旗下Skyhigh DSPM产品的Data Explorer中推出一款具备可执行性、智能化且聚焦风险的数据可视性与合规仪表板 。2025年4月&#xff0c;数据安全态势管理(DSP…

作者头像 李华
网站建设 2026/1/29 19:39:35

Wan2.2-T2V-A14B与Stable Video Diffusion对比:谁更适合商用?

Wan2.2-T2V-A14B与Stable Video Diffusion对比&#xff1a;谁更适合商用&#xff1f; 在AI生成内容&#xff08;AIGC&#xff09;浪潮席卷全球的今天&#xff0c;文本到视频&#xff08;Text-to-Video, T2V&#xff09;技术正从实验室走向生产线。广告、教育、电商、影视等行业…

作者头像 李华
网站建设 2026/1/26 17:17:25

如何用AU处理音乐详细的元数据Metadata-程序员·原创音乐人·卓伊凡

如何用AU处理音乐详细的元数据Metadata-程序员原创音乐人卓伊凡元数据&#xff08;Metadata&#xff09;里所有 AI 相关信息怎么删、怎么改音频本身通过 AU 做一些“人味处理”&#xff0c;尽量减少 AI 痕迹&#x1f9e9; 一、核心目标先说明白用 AU 能做的主要是两类事情&…

作者头像 李华
网站建设 2026/1/26 22:50:41

MobaXterm高效运维

MobaXterm简介与核心优势定义MobaXterm作为多功能远程管理工具的基本功能对比传统工具&#xff08;如PuTTY、Xshell&#xff09;突出其集成化优势支持协议与特性&#xff1a;SSH/RDP/VNC/X11转发/文件传输环境配置与基础操作便携版与安装版的选择策略会话管理&#xff1a;保存常…

作者头像 李华