Android 开发笔记___SQLite__优化记住密码功能
package com.example.alimjan.hello_world; /**
* Created by alimjan on 7/4/2017.
*/ import com.example.alimjan.hello_world.bean.UserInfo;
import com.example.alimjan.hello_world.dataBase.UserDBHelper;
import com.example.alimjan.hello_world.Utils.DateUtil; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener; public class class_4_2_3 extends AppCompatActivity implements OnClickListener, OnFocusChangeListener { private RadioGroup rg_login;
private RadioButton rb_password;
private RadioButton rb_verifycode;
private EditText et_phone;
private TextView tv_password;
private EditText et_password;
private Button btn_forget;
private CheckBox ck_remember;
private Button btn_login; private int mRequestCode = 0;
private int mType = 0;
private boolean bRemember = false;
private String mPassword = "111111";
private String mVerifyCode;
private UserDBHelper mHelper; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_4_2_3);
rg_login = (RadioGroup) findViewById(R.id.rg_login);
rb_password = (RadioButton) findViewById(R.id.rb_password);
rb_verifycode = (RadioButton) findViewById(R.id.rb_verifycode);
et_phone = (EditText) findViewById(R.id.et_phone);
tv_password = (TextView) findViewById(R.id.tv_password);
et_password = (EditText) findViewById(R.id.et_password);
btn_forget = (Button) findViewById(R.id.btn_forget);
ck_remember = (CheckBox) findViewById(R.id.ck_remember);
btn_login = (Button) findViewById(R.id.btn_login); rg_login.setOnCheckedChangeListener(new RadioListener());
ck_remember.setOnCheckedChangeListener(new CheckListener());
et_phone.addTextChangedListener(new HideTextWatcher(et_phone));
et_password.addTextChangedListener(new HideTextWatcher(et_password));
btn_forget.setOnClickListener(this);
btn_login.setOnClickListener(this);
et_password.setOnFocusChangeListener(this); ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
R.layout.item_select, typeArray);
typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
Spinner sp_type = (Spinner) findViewById(R.id.sp_type);
sp_type.setPrompt("请选择用户类型");
sp_type.setAdapter(typeAdapter);
sp_type.setSelection(mType);
sp_type.setOnItemSelectedListener(new TypeSelectedListener());
} private class RadioListener implements RadioGroup.OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb_password) {
tv_password.setText("登录密码:");
et_password.setHint("请输入密码");
btn_forget.setText("忘记密码");
ck_remember.setVisibility(View.VISIBLE);
} else if (checkedId == R.id.rb_verifycode) {
tv_password.setText(" 验证码:");
et_password.setHint("请输入验证码");
btn_forget.setText("获取验证码");
ck_remember.setVisibility(View.INVISIBLE);
}
}
} private String[] typeArray = {"个人用户", "公司用户"};
class TypeSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
mType = arg2;
} public void onNothingSelected(AdapterView<?> arg0) {
}
} private class CheckListener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.ck_remember) {
bRemember = isChecked;
}
}
} private class HideTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxLength;
private CharSequence mStr; public HideTextWatcher(EditText v) {
super();
mView = v;
mMaxLength = ViewUtil.getMaxLength(v);
} @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStr = s;
} @Override
public void afterTextChanged(Editable s) {
if (mStr == null || mStr.length() == 0)
return;
if ((mStr.length() == 11 && mMaxLength == 11) ||
(mStr.length() == 6 && mMaxLength == 6)) {
ViewUtil.hideOneInputMethod(class_4_2_3.this, mView);
}
}
} @Override
public void onClick(View v) {
String phone = et_phone.getText().toString();
if (v.getId() == R.id.btn_forget) {
if (phone==null || phone.length()<11) {
Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
if (rb_password.isChecked() == true) {
Intent intent = new Intent(this, class_4_2_3_1.class);
intent.putExtra("phone", phone);
startActivityForResult(intent, mRequestCode);
} else if (rb_verifycode.isChecked() == true) {
mVerifyCode = String.format("%06d", (int)(Math.random()*1000000%1000000));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请记住验证码");
builder.setMessage("手机号"+phone+",本次验证码是"+mVerifyCode+",请输入验证码");
builder.setPositiveButton("好的", null);
AlertDialog alert = builder.create();
alert.show();
}
} else if (v.getId() == R.id.btn_login) {
if (phone==null || phone.length()<11) {
Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
if (rb_password.isChecked() == true) {
if (et_password.getText().toString().equals(mPassword) != true) {
Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
return;
} else {
loginSuccess();
}
} else if (rb_verifycode.isChecked() == true) {
if (et_password.getText().toString().equals(mVerifyCode) != true) {
Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
return;
} else {
loginSuccess();
}
}
}
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == mRequestCode && data!=null) {
//用户密码已改为新密码
mPassword = data.getStringExtra("new_password");
}
} //从修改密码页面返回登录页面,要清空密码的输入框
@Override
protected void onRestart() {
et_password.setText("");
super.onRestart();
} @Override
protected void onResume() {
super.onResume();
mHelper = UserDBHelper.getInstance(this, 2);
mHelper.openWriteLink();
} @Override
protected void onPause() {
super.onPause();
mHelper.closeLink();
} private void loginSuccess() {
String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面",
et_phone.getText().toString(), typeArray[mType]);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("登录成功");
builder.setMessage(desc);
builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("我再看看", null);
AlertDialog alert = builder.create();
alert.show(); if (bRemember) {
UserInfo info = new UserInfo();
info.phone = et_phone.getText().toString();
info.password = et_password.getText().toString();
info.update_time = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss");
mHelper.insert(info);
}
} //为什么光标进入密码框事件不选onClick?因为要点两下才会触发onClick动作(第一下是切换焦点动作)
@Override
public void onFocusChange(View v, boolean hasFocus) {
String phone = et_phone.getText().toString();
if (v.getId() == R.id.et_password) {
if (phone.length() > 0 && hasFocus == true) {
UserInfo info = mHelper.queryByPhone(phone);
if (info != null) {
et_password.setText(info.password);
}else{
et_password.setText("");
}
}
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_4_2_3.class);
mContext.startActivity(intent);
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="5dp" > <RadioGroup
android:id="@+id/rg_login"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_password"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="true"
android:gravity="left|center"
android:text="密码登录"
android:textColor="@color/black"
android:textSize="17sp" /> <RadioButton
android:id="@+id/rb_verifycode"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:checked="false"
android:gravity="left|center"
android:text="验证码登录"
android:textColor="@color/black"
android:textSize="17sp" />
</RadioGroup> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text=" 我是:"
android:textColor="@color/black"
android:textSize="17sp" /> <Spinner
android:id="@+id/sp_type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_type"
android:gravity="left|center"
android:spinnerMode="dialog" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="手机号码:"
android:textColor="@color/black"
android:textSize="17sp" /> <EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_phone"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入手机号码"
android:inputType="number"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="登录密码:"
android:textColor="@color/black"
android:textSize="17sp" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_password" > <EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入密码"
android:inputType="numberPassword"
android:maxLength="6"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" /> <Button
android:id="@+id/btn_forget"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="center"
android:text="忘记密码"
android:textColor="@color/black"
android:textSize="17sp" />
</FrameLayout>
</RelativeLayout> <CheckBox
android:id="@+id/ck_remember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:checked="false"
android:padding="10dp"
android:text="记住密码"
android:textColor="@color/black"
android:textSize="17sp" /> <Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/black"
android:textSize="22sp" /> </LinearLayout>
package com.example.alimjan.hello_world; /**
* Created by alimjan on 7/4/2017.
*/ import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast; public class class_4_2_3_1 extends AppCompatActivity implements OnClickListener { private EditText et_password_first;
private EditText et_password_second;
private EditText et_verifycode;
private String mVerifyCode;
private String mPhone; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_4_2_3_1);
et_password_first = (EditText) findViewById(R.id.et_password_first);
et_password_second = (EditText) findViewById(R.id.et_password_second);
et_verifycode = (EditText) findViewById(R.id.et_verifycode);
findViewById(R.id.btn_verifycode).setOnClickListener(this);
findViewById(R.id.btn_confirm).setOnClickListener(this);
mPhone = getIntent().getStringExtra("phone");
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_verifycode) {
if (mPhone==null || mPhone.length()<11) {
Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
mVerifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请记住验证码");
builder.setMessage("手机号"+mPhone+",本次验证码是"+mVerifyCode+",请输入验证码");
builder.setPositiveButton("好的", null);
AlertDialog alert = builder.create();
alert.show();
} else if (v.getId() == R.id.btn_confirm) {
String password_first = et_password_first.getText().toString();
String password_second = et_password_second.getText().toString();
if (password_first==null || password_first.length()<6 ||
password_second==null || password_second.length()<6) {
Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show();
return;
}
if (password_first.equals(password_second) != true) {
Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
return;
}
if (et_verifycode.getText().toString().equals(mVerifyCode) != true) {
Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
return;
} else {
Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("new_password", password_first);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_4_2_3_1.class);
mContext.startActivity(intent);
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="5dp" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_password_first"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="输入新密码:"
android:textColor="@color/black"
android:textSize="17sp" /> <EditText
android:id="@+id/et_password_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_password_first"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入新密码"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_password_second"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="确认新密码:"
android:textColor="@color/black"
android:textSize="17sp" /> <EditText
android:id="@+id/et_password_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_password_second"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请再次输入新密码"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" />
</RelativeLayout> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" > <TextView
android:id="@+id/tv_verifycode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text=" 验证码:"
android:textColor="@color/black"
android:textSize="17sp" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_verifycode" > <EditText
android:id="@+id/et_verifycode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/editext_selector"
android:gravity="left|center"
android:hint="请输入验证码"
android:inputType="numberPassword"
android:maxLength="6"
android:textColor="@color/black"
android:textColorHint="@color/grey"
android:textCursorDrawable="@drawable/text_cursor"
android:textSize="17sp" /> <Button
android:id="@+id/btn_verifycode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:gravity="center"
android:text="获取验证码"
android:textColor="@color/black"
android:textSize="17sp" />
</FrameLayout>
</RelativeLayout> <Button
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="@color/black"
android:textSize="22sp" /> </LinearLayout>
当输入完手机号之后,点击密码编辑框时,从数据库查看内容,如果含有该号的密码则自动添加,如果没有则空。勾选记住密码选项之后,如果登陆成功则保存到数据库。
Android 开发笔记___SQLite__优化记住密码功能的更多相关文章
- Android 开发笔记___SQLite__基本用法
SQLiteOpenHelper package com.example.alimjan.hello_world.dataBase; import android.content.ContentVal ...
- android: SharedPreferences实现记住密码功能
既然是实现记住密码的功能,那么我们就不需要从头去写了,因为在上一章中的最佳实 践部分已经编写过一个登录界面了,有可以重用的代码为什么不用呢?那就首先打开 BroadcastBestPractice 项 ...
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- [置顶] Android开发笔记(成长轨迹)
分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:概述
1.前言 数据生产和数据展示是常见的两大专业级移动GIS应用场景,这里我们针对数据生产环节的ArcGIS的离在线一体化技术给大家做一个基本的介绍和梳理. 使用ArcGIS离在线一体化技术首先需要以下基 ...
- 《ArcGIS Runtime SDK for Android开发笔记》——离在线一体化技术:离线矢量数据同步
1.前言 上一篇文章中我们实现了离线要素的编辑操作,这一篇中主要介绍离在线一体化技术中最后一个环节离线数据的同步功能,通过对数据的上传,服务器端的版本化管理,实现数据生产管理的整个流程. 转载请注明出 ...
- java实现记住密码功能(利用cookie)
<br> <input type="text" id="userName" name="userName" value=& ...
随机推荐
- JAVA设计模式总结之六大设计原则
从今年的七月份开始学习设计模式到9月底,设计模式全部学完了,在学习期间,总共过了两篇:第一篇看完设计模式后,感觉只是脑子里面有印象但无法言语.于是决定在看一篇,到9月份第二篇设计模式总于看完了,这一篇 ...
- 如何安装和配置 Rex-Ray?- 每天5分钟玩转 Docker 容器技术(74)
Rex-Ray 是一个优秀的 Docker volume driver,本节将演示其安装和配置方法. Rex-Ray 以 standalone 进程的方式运行在 Docker 主机上,安装方法很简单, ...
- Redisson分布式锁的简单使用
一:前言 我在实际环境中遇到了这样一种问题,分布式生成id的问题!因为业务逻辑的问题,我有个生成id的方法,是根据业务标识+id当做唯一的值! 而uuid是递增生成的,从1开始一直递增,那么在同一台机 ...
- HelloWorld改编,仿bilibili手机端(一)——侧滑菜单界面布局
讲解目录: 1.要实现的效果图展示及详细分析HelloWorld项目的xml布局文件(基于navigation drawer activity) 2.简单修改menu及menu相关详解 ...
- Python自学笔记-列表生成式(来自廖雪峰的官网Python3)
感觉廖雪峰的官网http://www.liaoxuefeng.com/里面的教程不错,所以学习一下,把需要复习的摘抄一下. 以下内容主要为了自己复习用,详细内容请登录廖雪峰的官网查看. 列表生成式 列 ...
- 使用Travis CI自动部署Hexo到GitHub
原文链接(转载请注明出处):使用Travis CI自动部署Hexo到GitHub 前言 使用 hexo + gitPages 搭建个人博客的人都知道,每当要发表一篇博文,第一步得手动使用 hexo g ...
- ABP增删改查代码片段
@using System.Web.Optimization @using MultiPageSimpleTask.Entitys.Dtos; @model IList<ProductDto&g ...
- ActiveMQ——activemq的安装详情,修改密码
1.安装 下载 http://activemq.apache.org/download-archives.html, [推荐]ActiveMQ 5.13.4 Release与jdk1.7搭配(其它版本 ...
- break和continue 的区别
区别 break和continue都可在循环语句里面使用,也都可以控制外层的循环.但是continue只能在循环语句里面使用,break也可以使用在switch语句里面. break具体作用在循环语句 ...
- 关于AOP装饰函数中的this
在学习关于JavaScript的装饰者设计模式的过程中,发现其实现方式的关键在于this的使用. 想象一个匿名函数(其实预定义的有名函数也可以,都存在引用),其中的this: // 我们先定义一个匿名 ...