Android笔记(四十四) Android中的数据存储——SQLite(六)整合
实现注册、登录、注销账户
MainActivity.java
- package cn.lixyz.activity;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import cn.lixyz.data.MySQLiteOpenHelper;
- import cn.lixyz.sqlite.R;
- public class MainActivity extends Activity {
- // 声明组件
- private EditText et_login_username, et_login_password;
- private Button bt_login_login, bt_login_register;
- private CheckBox cb_login_rember;
- private TextView tv_login_forget;
- // 声明一个意图
- private Intent intent;
- // 声明SQLite相关内容
- private MySQLiteOpenHelper msop;
- private SQLiteDatabase database;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 找到组件
- findView();
- // 创建数据库
- msop = new MySQLiteOpenHelper(this, "user.db", null, 1);
- // 创建或打开一个数据库有两种方法,getWritableDatabase()和getReadableDatabase(),二者的区别是:
- // getWritableDatabase取得的实例是以读写的方式打开数据库,如果打开的数据库磁盘满了,此时只能读不能写,此时调用了getWritableDatabase的实例,那么将会发生错误(异常)
- // getReadableDatabase取得的实例是先调用getWritableDatabase以读写的方式打开数据库,如果数据库的磁盘满了,此时返回打开失败,继而用getReadableDatabase的实例以只读的方式去打开数据库
- database = msop.getReadableDatabase();
- }
- // 将findViewById独立封装,以清晰代码结构
- private void findView() {
- et_login_username = (EditText) findViewById(R.id.et_login_username);
- et_login_password = (EditText) findViewById(R.id.et_login_password);
- bt_login_login = (Button) findViewById(R.id.bt_login_login);
- bt_login_register = (Button) findViewById(R.id.bt_login_register);
- cb_login_rember = (CheckBox) findViewById(R.id.cb_login_rember);
- tv_login_forget = (TextView) findViewById(R.id.tv_login_forget);
- }
- // 通过ID来判断点击的是哪个按钮
- public void clickButton(View view) {
- switch (view.getId()) {
- case R.id.bt_login_login:
- login();
- break;
- case R.id.bt_login_register:
- register();
- break;
- case R.id.tv_login_forget:
- forgetPassword();
- break;
- }
- }
- // 登录方法
- private void login() {
- String username = et_login_username.getText().toString().trim();
- String password = et_login_password.getText().toString().trim();
- Cursor c = database.rawQuery("select * from user where username=? and password=?",
- new String[] { username, password });
- if (c.moveToFirst()) {
- intent = new Intent(MainActivity.this, IndexActivity.class);
- startActivity(intent);
- finish();
- } else {
- Toast.makeText(MainActivity.this, "登录不成功,检查您的账户和密码", Toast.LENGTH_SHORT).show();
- }
- c.close();
- }
- // 注册方法
- private void register() {
- intent = new Intent(MainActivity.this, RegesiterActivity.class);
- startActivity(intent);
- }
- // 找回密码方法
- private void forgetPassword() {
- intent = new Intent(MainActivity.this, ForgetPasswordActivity.class);
- startActivity(intent);
- }
- }
activity_main.xml
- <!-- 首页布局 -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <!-- 一个ImageView控件放置logo -->
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="15dp"
- android:src="@drawable/logo" />
- <!-- 两个EditText,输入用户名和密码 -->
- <EditText
- android:id="@+id/et_login_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的用户名" />
- <EditText
- android:id="@+id/et_login_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的密码"
- android:password="true" />
- <!-- 一个水平的线性布局,用来放置登录和注册按钮 -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/bt_login_login"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:onClick="clickButton"
- android:text="登录" />
- <Button
- android:id="@+id/bt_login_register"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:onClick="clickButton"
- android:text="注册" />
- </LinearLayout>
- <!-- 一个水平的线性布局,用来放置记住用户名和忘记密码 -->
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <CheckBox
- android:id="@+id/cb_login_rember"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:text="记住用户名" />
- <!-- 将weight设置为1,全占剩余空间,以保障忘记密码水平居右 -->
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <TextView
- android:id="@+id/tv_login_forget"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:onClick="clickButton"
- android:text="忘记密码 " />
- </LinearLayout>
- </LinearLayout>
RegesiterActivity.java
- package cn.lixyz.activity;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import cn.lixyz.data.MySQLiteOpenHelper;
- import cn.lixyz.sqlite.R;
- public class RegesiterActivity extends Activity {
- // 声明组件
- private EditText et_reg_username, et_reg_password, et_reg_againPassword, et_reg_mobilephone;
- private Button bt_reg_submit;
- // 声明要插入的数据变量
- private String username, password, mobilephone;
- // 声明数据库组件
- private MySQLiteOpenHelper msoh;
- private SQLiteDatabase database;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- findView();
- msoh = new MySQLiteOpenHelper(RegesiterActivity.this, "user.db", null, 1);
- database = msoh.getReadableDatabase();
- bt_reg_submit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String str = et_reg_againPassword.getText().toString().trim();
- String str2 = et_reg_password.getText().toString().trim();
- // 判断密码是否输入一致,如果不一致,弹出提示,并清空输入的密码
- if (str.equals(str2)) {
- password = str;
- } else {
- Toast.makeText(RegesiterActivity.this, "您输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
- et_reg_password.setText("");
- et_reg_againPassword.setText("");
- return;
- }
- username = et_reg_username.getText().toString().trim();
- mobilephone = et_reg_mobilephone.getText().toString().trim();
- // 打包插入数据
- ContentValues cv = new ContentValues();
- cv.put("username", username);
- cv.put("password", password);
- cv.put("mobilephone", mobilephone);
- if (database.insert("user", null, cv) > 0) {
- Toast.makeText(RegesiterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
- finish();
- } else {
- Toast.makeText(RegesiterActivity.this, "注册不成功", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- //// 将findViewById独立封装,以清晰代码结构
- private void findView() {
- et_reg_username = (EditText) findViewById(R.id.et_reg_username);
- et_reg_password = (EditText) findViewById(R.id.et_reg_password);
- et_reg_againPassword = (EditText) findViewById(R.id.et_reg_againPassword);
- et_reg_mobilephone = (EditText) findViewById(R.id.et_reg_mobilephone);
- bt_reg_submit = (Button) findViewById(R.id.bt_reg_submit);
- }
- }
activity_register.xml
- <!-- 注册布局 -->
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <!-- 注册用户名 -->
- <EditText
- android:id="@+id/et_reg_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您要注册的用户名" />
- <!-- 注册密码 -->
- <EditText
- android:id="@+id/et_reg_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您要注册的密码"
- android:password="true" />
- <!-- 重复注册密码 -->
- <EditText
- android:id="@+id/et_reg_againPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="再次输入密码"
- android:password="true" />
- <!-- 注册手机号 -->
- <EditText
- android:id="@+id/et_reg_mobilephone"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的手机号" />
- <!-- 提交注册按钮 -->
- <Button
- android:id="@+id/bt_reg_submit"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:text="提 交" />
- </LinearLayout>
IndexActivity.java
- package cn.lixyz.activity;
- import android.app.Activity;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import cn.lixyz.data.MySQLiteOpenHelper;
- import cn.lixyz.sqlite.R;
- public class IndexActivity extends Activity {
- private EditText et_del_mobile;
- private Button bt_del_submit;
- private MySQLiteOpenHelper msop;
- private SQLiteDatabase database;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_index);
- findView();
- msop = new MySQLiteOpenHelper(IndexActivity.this, "user.db", null, 1);
- database = msop.getReadableDatabase();
- bt_del_submit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String mobile = et_del_mobile.getText().toString().trim();
- Cursor c = database.rawQuery("select mobilephone from user where mobilephone=?",
- new String[] { mobile });
- if (c.moveToFirst()) {
- int i = database.delete("user", "mobilephone=?", new String[] { mobile });
- if (i > 0) {
- Toast.makeText(IndexActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
- finish();
- } else {
- Toast.makeText(IndexActivity.this, "*删除不成功*", Toast.LENGTH_SHORT).show();
- }
- } else {
- Toast.makeText(IndexActivity.this, "删除不成功", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- private void findView() {
- et_del_mobile = (EditText) findViewById(R.id.et_del_mobile);
- bt_del_submit = (Button) findViewById(R.id.bt_del_submit);
- }
- }
activity_index.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="注销账户" />
- <EditText
- android:id="@+id/et_del_mobile"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="验证您的手机号" />
- <Button
- android:id="@+id/bt_del_submit"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="点我验证,删除账户" />
- </LinearLayout>
ForgetPasswordActivity.java
- package cn.lixyz.activity;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import cn.lixyz.data.MySQLiteOpenHelper;
- import cn.lixyz.sqlite.R;
- public class ForgetPasswordActivity extends Activity {
- private EditText et_forget_mobile, et_forget_username, et_forget_password, et_forget_againPassword;
- private Button bt_forget_submit;
- private MySQLiteOpenHelper msop;
- private SQLiteDatabase database;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_forget);
- findView();
- msop = new MySQLiteOpenHelper(this, "user.db", null, 1);
- database = msop.getReadableDatabase();
- bt_forget_submit.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String username = et_forget_username.getText().toString().trim();
- String mobile = et_forget_mobile.getText().toString().trim();
- Cursor c = database.rawQuery("select username,mobilephone from user where username=? and mobilephone=?",
- new String[] { username, mobile });
- if (c.moveToFirst()) {
- String str = et_forget_password.getText().toString().trim();
- String str2 = et_forget_againPassword.getText().toString().trim();
- if (str.equals(str2)) {
- ContentValues cv = new ContentValues();
- cv.put("password", str);
- database.update("user", cv, "username=? and mobilephone=?", new String[] { username, mobile });
- Toast.makeText(ForgetPasswordActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
- finish();
- }
- }
- }
- });
- }
- private void findView() {
- et_forget_mobile = (EditText) findViewById(R.id.et_forget_mobile);
- et_forget_username = (EditText) findViewById(R.id.et_forget_username);
- et_forget_password = (EditText) findViewById(R.id.et_forget_password);
- et_forget_againPassword = (EditText) findViewById(R.id.et_forget_againPassword);
- bt_forget_submit = (Button) findViewById(R.id.bt_forget_submit);
- }
- }
activity_forget.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/et_forget_mobile"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的手机号" />
- <EditText
- android:id="@+id/et_forget_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的用户名" />
- <EditText
- android:id="@+id/et_forget_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="输入您的密码" />
- <EditText
- android:id="@+id/et_forget_againPassword"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:hint="重复输入您的密码" />
- <Button
- android:id="@+id/bt_forget_submit"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:text="修改" />
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.lixyz.sqlite"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="19"
- android:targetSdkVersion="19" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="cn.lixyz.activity.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name="cn.lixyz.activity.RegesiterActivity" />
- <activity android:name="cn.lixyz.activity.IndexActivity" />
- <activity android:name="cn.lixyz.activity.ForgetPasswordActivity" />
- </application>
- </manifest>
Android笔记(四十四) Android中的数据存储——SQLite(六)整合的更多相关文章
- Android笔记(四十二) Android中的数据存储——SQLite(四)update
update方法的四个参数: update()方法参数 对应的sql部分 描述 table update table_name 更新的表名 values set column=xxx ContentV ...
- Android笔记(四十) Android中的数据存储——SQLite(二) insert
准备工作: 我们模拟一个注册的页面,先看UI 我们需要创建一个数据库:user,数据库包含表user,user表包含字段id.username.password.mobilephone MainAct ...
- Android笔记(十四) Android中的基本组件——按钮
Android中的按钮主要包括Button和ImageButton两种,Button继承自TextView,而ImageButton继承自ImageView.Button生成的按钮上显示文字,而Ima ...
- Android笔记二十四.Android基于回调的事件处理机制
假设说事件监听机制是一种托付式的事件处理,那么回调机制则与之相反,对于基于回调的事件处理模型来说,事件源和事件监听器是统一的,或者说事件监听器全然消失了,当用户在GUI控件上激发某个事件时,控 ...
- Android笔记三十四.Service综合实例二
综合实例2:client訪问远程Service服务 实现:通过一个button来获取远程Service的状态,并显示在两个文本框中. 思路:如果A应用须要与B应用进行通信,调用B应用中的getName ...
- Android笔记(七十四) 详解Intent
我们最常使用Intent来实现Activity之间的转跳,最近做一个app用到从系统搜索图片的功能,使用到了intent的 setType 方法和 setAction 方法,网上搜索一番,发现实现转跳 ...
- Android笔记(四十一) Android中的数据存储——SQLite(三)select
SQLite 通过query实现查询,它通过一系列参数来定义查询条件. 各参数说明: query()方法参数 对应sql部分 描述 table from table_name 表名称 colums s ...
- Android笔记(四十三) Android中的数据存储——SQLite(五)delete
SQLite通过delete()方法删除数据 delete()方法参数说明: delete()方法参数 对应sql部分 描述 table delte from table_name 要删除的表 whe ...
- Android笔记(七十五) Android中的图片压缩
这几天在做图记的时候遇第一次遇到了OOM,好激动~~ 追究原因,是因为在ListView中加载的图片太大造成的,因为我使用的都是手机相机直接拍摄的照片,图片都比较大,所以在加载的时候会出现内存溢出,那 ...
随机推荐
- linux设置程序运行超时时间
在某些情况下,我们需要限制程序的运行时间(比如cronjob等),这里简单介绍下使用信号及timeout的实现方法 1. 假如有如下代码(test_timout.sh): #!/bin/bash wh ...
- ISO/IEC 9899:2011 条款6.10.3——宏替换
6.10.3 宏替换 约束 1.两个替换列表是相同的,当且仅当两个替换列表中的预处理符记都具有相同的数.次序.拼写,以及空白分隔符,这里所有的空白分隔符都认为是相同的. 2.当前被定义为一个类似对象的 ...
- python初级(302) 4 函数
一.函数 1.函数定义: 可以完成某个工作的代码块.这是可以用来构建更大程序的一个小部分. 2.创建或定义函数要使用def关键字 3.创建一个函数 1) def 关键字 2)函数名及后面跟随的括号 3 ...
- 使用 Node.js 写一个代码生成器
背景 第一次接触代码生成器用的是动软代码生成器,数据库设计好之后,一键生成后端 curd代码.之后也用过 CodeSmith , T4.目前市面上也有很多优秀的代码生成器,而且大部分都提供可视化界面操 ...
- Python下numpy的使用
首先:当然是欢迎大家了! Numpy : NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structur ...
- 滚动条mCustomScrollbar自定义
mCustomScrollbar 是个基于 jQuery UI 的自定义滚动条插件,它可以让你灵活的通过 CSS 定义网页的滚动条,并且垂直和水平两个方向的滚动条都可以定义,它通过 Brandon A ...
- CEF4Delphi 常用设置
CEF4Delphi是由 SalvadorDíazFau 创建的一个开源项目,用于在基于Delphi的应用程序中嵌入基于Chromium的浏览器. CEF4Delphi 基于Henri Gourves ...
- 2. RDD编程
2.1 编程模型 在Spark中,RDD被表示为对象,通过对象上的方法调用来对RDD进行转换.经过一系列的transformations定义RDD之后,就可以调用actions触发RDD的计算,act ...
- 手贱删了一些不该删的东西,导致git pull每次都要输入账号密码...
解决办法: 输入一次账号密码,然后git工程目录下执行 git config --global credential.helper store 就妥了.
- [CF436D]Pudding Monsters
题目大意:有一个长度为$2\times 10^5$的板,有$n(n\leqslant 10^5)$个格子$a_1,\dots,a_n$有布丁怪兽,一开始连续的怪兽算一个怪兽,有$m(m\leqslan ...