基于sqlite的android登录app
该登录app主要包括三个模块:
1、登录:用户选择登录方式、登录身份,输入账号密码,完成登录。
2、忘记密码:用户输入新密码及验证码修改登录密码。
3、个人信息:用户完成登录后设置个人信息并显示。
使用控件:
1、单选按钮radiobutton:区分是密码登录还是验证码登录。
2、下拉框spinner:区分是个人用户还是公司用户。
3、编辑框edittext:输入手机号和密码(或验证码)。
4、复选框checkbox:判断是否记住密码。
5、相对布局relativelayout:界面的整体布局,方便将各个控件按照相对位置摆放。
6、框架布局framelayout:在框架布局中后面添加的子视图会把之前的子视图覆盖掉,一般用于需要重叠显示的场合。用于实现忘记密码按钮和密码输入框的叠加。
采用的存储方式
1、共享参数sharedpreferences:
是android的一个轻量级存储工具,采用的存储结构是key-value的键值对方式,类似于java的properties类,都是把key-value的键值对保存在配置文件中,不同的是properties的文件内容是key=value的形式,而sharedpreferences的存储介质是符合xml规范的配置文件。本案例中用于保存用户的账号和密码。
2、数据库sqlite:
是一个小巧的嵌入式数据库。本案例中用于存储用户的个人信息。
成果展示:
界面设计:
1. 登录界面
<?xml version="1.0" encoding="utf-8"?><relativelayout 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" tools:context=".loginactivity" android:paddingtop="10dp" android:padding="8dp"> <radiogroup android:id="@+id/rg_login_way" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margintop="20dp"> <radiobutton android:id="@+id/rb_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码登录" android:textsize="25sp" /> <radiobutton android:id="@+id/rb_checkcode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证码登录" android:layout_marginleft="50dp" android:textsize="25sp" /> </radiogroup> <textview android:id="@+id/tv_shenfen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是:" android:layout_below="@+id/rg_login_way" android:textsize="25sp" android:layout_margintop="40dp" android:textcolor="@color/black" android:layout_marginleft="30dp" android:layout_alignright="@+id/tv_phonenum"/> <spinner android:id="@+id/sp_shenfen" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_shenfen" android:layout_alignbottom="@+id/tv_shenfen" android:spinnermode="dialog"/> <textview android:id="@+id/tv_phonenum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_shenfen" android:text="手机号码:" android:textsize="25sp" android:textcolor="@color/black" android:layout_margintop="40dp"/> <edittext android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignbaseline="@id/tv_phonenum" android:layout_torightof="@+id/tv_phonenum" android:background="@drawable/eb_selector" android:textsize="25sp" android:hint="请输入手机号码" android:inputtype="number" /> <textview android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="40dp" android:text="登录密码:" android:layout_below="@id/tv_phonenum" android:textsize="25sp" android:layout_margintop="40dp" android:textcolor="@color/black"/> <framelayout android:id="@+id/fm_psw" android:layout_width="match_parent" android:layout_height="50dp" android:layout_torightof="@id/tv_psw" android:layout_alignbottom="@+id/tv_psw" android:layout_alignleft="@+id/et_phone"> <edittext android:id="@+id/et_psw" android:layout_width="match_parent" android:layout_height="match_parent" android:hint="请输入密码" android:textsize="25sp" android:background="@drawable/eb_selector" /> <button android:id="@+id/btn_pswforget" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="忘记密码" android:textsize="25sp" android:background="@color/darkgray" android:padding="10dp" android:layout_gravity="end"/> </framelayout> <checkbox android:id="@+id/cb_pswrmb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv_psw" android:text="记住密码" android:textsize="25sp" android:layout_margintop="30dp"/> <button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/cb_pswrmb" android:text="登录" android:textsize="25sp" android:layout_margintop="30dp" android:background="@color/darkgray"/></relativelayout>
2.忘记密码界面
<?xml version="1.0" encoding="utf-8"?><relativelayout 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:padding="8dp" android:paddingtop="10dp" tools:context=".pswforgetactivity"> <textview android:id="@+id/tv_newpsw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入新密码:" android:textcolor="@color/black" android:textsize="25sp" android:layout_margintop="20dp"/> <edittext android:id="@+id/et_newpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_newpsw" android:background="@drawable/eb_selector" android:layout_alignbaseline="@+id/tv_newpsw" android:hint="请输入新密码" android:textsize="25sp" android:inputtype="textpassword" /> <textview android:id="@+id/tv_chknewpsw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="40dp" android:text="确认新密码:" android:layout_below="@+id/tv_newpsw" android:textsize="25sp" android:textcolor="@color/black"/> <edittext android:id="@+id/et_chknewpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_chknewpsw" android:layout_alignbaseline="@+id/tv_chknewpsw" android:background="@drawable/eb_selector" android:textsize="25sp" android:hint="请再次输入新密码" android:inputtype="textpassword"/> <textview android:id="@+id/tv_checkcode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="验证码:" android:layout_below="@+id/tv_chknewpsw" android:textsize="25sp" android:textcolor="@color/black" android:layout_margintop="40dp"/> <framelayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_checkcode" android:layout_below="@+id/et_chknewpsw" android:layout_margintop="20dp"> <edittext android:id="@+id/et_checkcode" android:layout_width="match_parent" android:layout_height="match_parent" android:textsize="25sp" android:hint="输入验证码" android:inputtype="number" android:background="@drawable/eb_selector" android:maxlines="1"/> <button android:id="@+id/btn_sendcheckcode" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" android:text="获取验证码" android:textsize="25sp" android:padding="10dp" android:textcolor="@color/black" android:background="@color/darkgray"/> </framelayout> <button android:id="@+id/btn_check" android:layout_width="match_parent" android:layout_height="60dp" android:layout_below="@id/tv_checkcode" android:text="确定" android:textsize="25sp" android:layout_margintop="30dp" android:background="@color/darkgray" /></relativelayout>
3.个人信息填写界面
<?xml version="1.0" encoding="utf-8"?><relativelayout 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" tools:context=".sharedpreferencesactivity" android:padding="10dp"> <textview android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textsize="25sp" android:textcolor="@color/black" android:layout_margintop="20dp"/> <edittext android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_name" android:layout_alignbaseline="@+id/tv_name" android:background="@drawable/eb_selector" android:maxlines="1"/> <textview android:id="@+id/tv_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄:" android:textsize="25sp" android:textcolor="@color/black" android:layout_below="@+id/tv_name" android:layout_margintop="20dp"/> <edittext android:id="@+id/et_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_age" android:layout_alignbaseline="@+id/tv_age" android:background="@drawable/eb_selector" android:maxlines="1" android:inputtype="number"/> <textview android:id="@+id/tv_height" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="身高:" android:textsize="25sp" android:textcolor="@color/black" android:layout_below="@+id/tv_age" android:layout_margintop="20dp"/> <edittext android:id="@+id/et_height" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_height" android:layout_alignbaseline="@+id/tv_height" android:background="@drawable/eb_selector" android:maxlines="1" android:inputtype="number"/> <textview android:id="@+id/tv_weight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="体重:" android:textsize="25sp" android:textcolor="@color/black" android:layout_below="@+id/tv_height" android:layout_margintop="20dp"/> <edittext android:id="@+id/et_weight" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_torightof="@+id/tv_weight" android:layout_alignbaseline="@+id/tv_weight" android:background="@drawable/eb_selector" android:maxlines="1" android:inputtype="number"/> <textview android:id="@+id/tv_married" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="婚否:" android:layout_below="@+id/tv_weight" android:textsize="25sp" android:textcolor="@color/black" android:layout_margintop="20dp"/> <spinner android:id="@+id/sp_married" android:layout_width="match_parent" android:layout_height="40dp" android:spinnermode="dropdown" android:layout_torightof="@+id/tv_married" android:layout_alignbottom="@+id/tv_married"/> <button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/tv_married" android:layout_margintop="20dp" android:background="@drawable/selector" android:text="保存" android:textsize="25sp" android:textcolor="@color/black"/></relativelayout>
4.个人信息显示界面
<?xml version="1.0" encoding="utf-8"?><relativelayout 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" tools:context=".sharedpreferencesactivity2"> <textview android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/btn_delete" android:textsize="25sp"/> <button android:id="@+id/btn_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" android:textsize="25sp"/></relativelayout>
代码实现
userdbhelper
package com.example.helloworld;
import android.content.contentvalues;
import android.content.context;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqliteopenhelper;
import android.util.log;
import androidx.annotation.nullable;
import androidx.core.app.navutils;
import java.util.arraylist;
import java.util.locale;
public class userdbhelper extends sqliteopenhelper {
private static final string tag = "userdbhelper";
private static final string db_name = "user.db"; //数据库名
private static final int db_version = 1; //数据库版本
private static userdbhelper mhelper = null;
private sqlitedatabase mdb = null;
private static final string table_name = "user_info"; //表名
private userdbhelper(context context){
super(context,db_name,null,db_version);
}
private userdbhelper(context context,int version){
super(context,db_name,null,version);
}
public static userdbhelper getinstance(context context,int version){
if(version > 0 && mhelper == null){
mhelper = new userdbhelper(context,version);
}else if(mhelper == null){
mhelper = new userdbhelper(context);
}
return mhelper;
}
public sqlitedatabase openreadlink(){
if (mdb == null || !mdb.isopen()){
mdb = mhelper.getreadabledatabase();
}
return mdb;
}
public sqlitedatabase openwritelink(){
if (mdb == null || !mdb.isopen()){
mdb = mhelper.getwritabledatabase();
log.d(tag, "openwritelink: 打开了读数据库");
}
return mdb;
}
public void closelink(){
if (mdb != null && mdb.isopen()){
mdb.close();
mdb = null;
}
}
public string getdbname(){
if(mhelper != null){
return mhelper.getdatabasename();
}else {
return db_name;
}
}
@override
public void oncreate(sqlitedatabase db) {
log.d(tag, "oncreate: 创建数据库");
string drop_sql = "drop table if exists " + table_name + ";";
db.execsql(drop_sql);
string create_sql = "create table if not exists " + table_name + " ("
+ "_id integer primary key autoincrement not null,"
+ "name varchar not null,"
+ "age integer not null,"
+ "height long not null,"
+ "weight float not null,"
+ "married integer not null,"
+ "update_time varchar not null,"
+ "phone varchar not null,"
+ "password varchar not null"
+ ");";
log.d(tag, "create_sql" + create_sql);
db.execsql(create_sql);
}
@override
public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {
log.d(tag, "onupgrade oldversion=" +oldversion+",newversion=" + newversion+"数据库新旧版本号");
if (newversion > 1){
string alter_sql = "alter table" + table_name + "add column" + "phone varchar;";
log.d(tag, "alter_sql:" + alter_sql);
db.execsql(alter_sql);
alter_sql = "alter table" + table_name + "add column" + "password varchar;";
log.d(tag, "alter_sql:" + alter_sql);
db.execsql(alter_sql);
}
}
public int delete(string condition){
int count = mdb.delete(table_name,condition,null);
return count;
}
public int deleteall(){
int count = mdb.delete(table_name,"1=1",null);
return count;
}
public long insert(userinfo info){
arraylist<userinfo> infoarray = new arraylist<userinfo>();
infoarray.add(info);
return insert(infoarray);
}
public arraylist<userinfo>query(string condition) {
string sql = string.format(locale.china,"select rowid,_id,name,age,height,weight,married,update_time," + "phone,password from %s where %s;", table_name,condition);
log.d(tag, "query sql: " + sql);
arraylist<userinfo> infoarray = new arraylist<userinfo>();
cursor cursor = mdb.rawquery(sql, null);
while (cursor.movetonext()) {
userinfo info = new userinfo();
info.rowid = cursor.getlong(0);
info.xuhao = cursor.getint(1);
info.name = cursor.getstring(2);
info.age = cursor.getint(3);
info.height = cursor.getlong(4);
info.weight = cursor.getfloat(5);
info.married = (cursor.getint(6) == 0) ? false : true;
info.update_time = cursor.getstring(7);
info.phone = cursor.getstring(8);
info.password = cursor.getstring(9);
infoarray.add(info);
}
cursor.close();
return infoarray;
}
public long insert(arraylist<userinfo> infoarray) {
long result = -1;
for (int i = 0; i < infoarray.size(); i++) {
userinfo info = infoarray.get(i);
arraylist<userinfo> temparray = new arraylist<userinfo>();
if (info.name != null && info.name.length() > 0) {
string condition = string.format("name='%s'", info.name);
temparray = query(condition);
if (temparray.size() > 0) {
update(info, condition);
result = temparray.get(0).rowid;
continue;
}
}
if (info.phone != null && info.phone.length() > 0) {
string condition = string.format("phone='%s'", info.phone);
temparray = query(condition);
if (temparray.size() > 0) {
update(info, condition);
result = temparray.get(0).rowid;
continue;
}
}
log.d(tag, "insert: 当前版本号"+mdb.getversion());
contentvalues cv = new contentvalues();
cv.put("name", info.name);
cv.put("age", info.age);
cv.put("height", info.height);
cv.put("weight", info.weight);
cv.put("married", info.married);
cv.put("update_time", info.update_time);
cv.put("phone", info.phone);
cv.put("password", info.password);
result = mdb.insert(table_name, "", cv);
if (result == -1) {
return result;
}
}
return result;
}
public int update(userinfo info, string condition) {
contentvalues cv = new contentvalues();
cv.put("name", info.name);
cv.put("age", info.age);
cv.put("height", info.height);
cv.put("weight", info.weight);
cv.put("married", info.married);
cv.put("update_time", info.update_time);
cv.put("phone", info.phone);
cv.put("password", info.password);
// 执行更新记录动作,该语句返回记录更新的数目
return mdb.update(table_name, cv, condition, null);
}
public int update(userinfo info) {
return update(info, "rowid=" + info.rowid);
}
public userinfo querybyphone(string phone){
userinfo info = null;
arraylist<userinfo> infoarray = query(string.format("phone=%s",phone));
if (infoarray.size() > 0 ){
info = infoarray.get(0);
}
return info;
}
}
userinfo
package com.example.helloworld;
public class userinfo {
public long rowid;
public int xuhao;
public string name;
public int age;
public long height;
public float weight;
public boolean married;
public string update_time;
public string phone;
public string password;
public userinfo() {
rowid = 0l;
xuhao = 0;
name = "";
age = 0;
height = 0l;
weight = 0.0f;
married = false;
update_time = "";
phone = "";
password = "";
}
}
loginactivity
package com.example.helloworld;
import androidx.annotation.nullable;
import androidx.appcompat.app.alertdialog;
import androidx.appcompat.app.appcompatactivity;
import android.content.context;
import android.content.dialoginterface;
import android.content.intent;
import android.content.sharedpreferences;
import android.graphics.rect;
import android.os.bundle;
import android.text.inputtype;
import android.text.method.passwordtransformationmethod;
import android.text.method.transformationmethod;
import android.util.log;
import android.view.view;
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.toast;
import java.net.passwordauthentication;
public class loginactivity extends appcompatactivity implements view.onclicklistener {
private edittext et_phone;
private radiobutton rb_psw;
private radiobutton rb_checkcode;
private edittext et_psw;
private button btn_pswforget;
private string mpassword;
private int mrequestcode;
private string mcheckcode;
private int mtype;
private string tag = "huahua";
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_login);
rb_psw = findviewbyid(r.id.rb_password);
rb_checkcode = findviewbyid(r.id.rb_checkcode);
et_phone = findviewbyid(r.id.et_phone);
et_psw = findviewbyid(r.id.et_psw);
checkbox cb_pswforget = findviewbyid(r.id.cb_pswrmb);
button btn_login = findviewbyid(r.id.btn_login);
btn_pswforget = findviewbyid(r.id.btn_pswforget);
btn_login.setonclicklistener(this);
btn_pswforget.setonclicklistener(this);
radiogroup rg = findviewbyid(r.id.rg_login_way);
mpassword = et_psw.gettext().tostring();
rg.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
radiobutton rb = findviewbyid(checkedid);
log.i(tag, "oncheckedchanged: 密码登录"+ rb_psw.ischecked());
log.i(tag, "oncheckedchanged: 验证码登录"+rb_checkcode.ischecked());
if(rb_psw.ischecked()){
et_psw.setinputtype(inputtype.type_text_variation_password | inputtype.type_class_text);
}
}
});
cb_pswforget.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {
@override
public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
}
});
arrayadapter<string> typeadapter = new arrayadapter<string>(this,r.layout.item_dropdown,typearray);
typeadapter.setdropdownviewresource(r.layout.item_dropdown);
spinner sp_type = findviewbyid(r.id.sp_shenfen);
sp_type.setadapter(typeadapter);
sp_type.setselection(0);
sp_type.setprompt("选择你的登录身份:");
sp_type.setonitemselectedlistener(new myonitemseclectedlistener());
}
private string[] typearray = {"个人用户","企业用户"};
private class myonitemseclectedlistener implements adapterview.onitemselectedlistener{
@override
public void onitemselected(adapterview<?> parent, view view, int position, long id) {
mtype = position;
}
@override
public void onnothingselected(adapterview<?> parent) {
}
}
@override
protected void onactivityresult(int requestcode, int resultcode, @nullable intent data) {
super.onactivityresult(requestcode, resultcode, data);
if (requestcode == mrequestcode && data != null) {
mpassword = data.getstringextra("newpsw");
}
}
@override
protected void onrestart() {
et_psw.settext("");
super.onrestart();
}
@override
public void onclick(view v) {
string phone = et_phone.gettext().tostring();
switch (v.getid()){
case r.id.btn_pswforget: {
log.i(tag, "onclick: 点击了忘记密码");
if (phone == null || phone.length() < 11) {
toast.maketext(this, "请输入正确的手机号", toast.length_short).show();
return;
}
if (rb_psw.ischecked()) {
log.i(tag, "onclick: 进入忘记密码界面");
intent intent = new intent(this, pswforgetactivity.class);
intent.putextra("phone", phone);
startactivityforresult(intent, mrequestcode);
} else if (rb_checkcode.ischecked()) {
mcheckcode = string.format("%06d", (int) (math.random() * 1000000 % 1000000));
log.i(tag, "onclick: 发送验证码");
alertdialog.builder builder = new alertdialog.builder(this);
builder.settitle("请记住验证码!");
builder.setmessage("手机号" + phone + ",本次验证码是:" + mcheckcode + ",请输入验证码");
builder.setpositivebutton("确定", null);
alertdialog alert = builder.create();
alert.show();
}
}
break;
case r.id.btn_login: {
if (phone == null || phone.length() < 11) {
toast.maketext(this, "请输入正确的手机号!", toast.length_short).show();
log.i(tag, "onclick: 验证密码");
return;
}
if (rb_psw.ischecked()) {
if (!et_psw.gettext().tostring().equals(mpassword) || et_psw.gettext().tostring().equals("")) {
toast.maketext(this, "请输入正确的密码", toast.length_short).show();
return;
} else {
loginsuccess();
}
} else if (rb_checkcode.ischecked()) {
if (!et_psw.gettext().tostring().equals(mcheckcode)) {
toast.maketext(this, "请输入正确的验证码", toast.length_short).show();
return;
} else {
loginsuccess();
}
}
}
}
}
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) {
intent intent = new intent(loginactivity.this,sharedpreferencesactivity.class);
startactivity(intent);
sharedpreferences sps = getsharedpreferences("login", context.mode_private);
sharedpreferences.editor editor = sps.edit();
editor.putstring("phone",et_phone.gettext().tostring());
editor.putstring("password",et_psw.gettext().tostring());
editor.apply();
}
});
builder.setnegativebutton("取消",null);
alertdialog alert = builder.create();
alert.show();
}
}
pswforgetactivity
package com.example.helloworld;
import androidx.appcompat.app.alertdialog;
import androidx.appcompat.app.appcompatactivity;
import android.app.activity;
import android.content.intent;
import android.os.bundle;
import android.text.inputtype;
import android.view.view;
import android.widget.adapterview;
import android.widget.edittext;
import android.widget.toast;
public class pswforgetactivity extends appcompatactivity implements view.onclicklistener {
private edittext et_newpsw;
private edittext et_chknewpsw;
private edittext et_checkcode;
private string mcheckcode;
private string mphone;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_psw_forget);
et_newpsw = findviewbyid(r.id.et_newpsw);
et_chknewpsw = findviewbyid(r.id.et_chknewpsw);
et_checkcode = findviewbyid(r.id.et_checkcode);
findviewbyid(r.id.btn_sendcheckcode).setonclicklistener(this);
findviewbyid(r.id.btn_check).setonclicklistener(this);
mphone = getintent().getstringextra("phone");
et_newpsw.setinputtype(inputtype.type_text_variation_password | inputtype.type_class_text);
et_chknewpsw.setinputtype(inputtype.type_text_variation_password | inputtype.type_class_text);
}
@override
public void onclick(view v) {
switch (v.getid()){
case r.id.btn_sendcheckcode:
if(mphone == null || mphone.length() < 11){
toast.maketext(this,"请输入正确的手机号",toast.length_short).show();
return;
}
mcheckcode = string.format("%06d",(int)(math.random()*1000000%1000000));
alertdialog.builder builder = new alertdialog.builder(this);
builder.settitle("请记住验证码");
builder.setmessage("手机号"+mphone+",本次验证码是"+mcheckcode+",请输入验证码");
builder.setpositivebutton("确定",null);
alertdialog alertdialog = builder.create();
alertdialog.show();
case r.id.btn_check:
string newpsw = et_newpsw.gettext().tostring();
string chknewpsw = et_chknewpsw.gettext().tostring();
if(newpsw == null || newpsw.length() < 6 || chknewpsw == null || chknewpsw.length() < 6){
toast.maketext(this,"请输入正确的新密码",toast.length_short).show();
return;
}else if(!newpsw.equals(chknewpsw)){
toast.maketext(this,"两次输入的新密码不一致",toast.length_short).show();
return;
}else if(!et_checkcode.gettext().tostring().equals(mcheckcode)){
toast.maketext(this,"请输入正确的验证码",toast.length_short).show();
return;
}else {
toast.maketext(this,"密码修改成功",toast.length_short).show();
intent intent = new intent();
intent.putextra("newpsw",newpsw);
setresult(activity.result_ok,intent);
finish();
}
}
}
}
infowriteactivity
package com.example.helloworld;
import androidx.appcompat.app.appcompatactivity;
import android.content.context;
import android.content.intent;
import android.content.sharedpreferences;
import android.database.sqlite.sqlitedatabase;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.edittext;
import android.widget.spinner;
import android.widget.toast;
public class infowriteactivity extends appcompatactivity implements view.onclicklistener {
private static final string tag = "huahua";
private userdbhelper mhelper;
private edittext et_name;
private edittext et_age;
private edittext et_height;
private edittext et_weight;
private boolean married = false;
private string phone;
private string password;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_shared_preferences);
et_name = findviewbyid(r.id.et_name);
et_age = findviewbyid(r.id.et_age);
et_height = findviewbyid(r.id.et_height);
et_weight = findviewbyid(r.id.et_weight);
findviewbyid(r.id.btn_save).setonclicklistener(this);
sharedpreferences sps = getsharedpreferences("login",context.mode_private);
sharedpreferences.editor editor = sps.edit();
phone = sps.getstring("phone","");
password = sps.getstring("password","");
arrayadapter<string> typeadapter = new arrayadapter<string>(this, r.layout.item_dropdown, typearray);
typeadapter.setdropdownviewresource(r.layout.item_dropdown);
spinner sp_married = findviewbyid(r.id.sp_married);
sp_married.setadapter(typeadapter);
sp_married.setprompt("请选择婚姻状况");
sp_married.setselection(0);
sp_married.setonitemselectedlistener(new typeselectedlistener());
}
private string[] typearray = {"未婚", "已婚"};
class typeselectedlistener implements adapterview.onitemselectedlistener {
public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) {
married = (arg2 == 0) ? false : true;
}
public void onnothingselected(adapterview<?> arg0) {
}
}
@override
protected void onstart() {
super.onstart();
sqlitedatabase mdb = getapplicationcontext().openorcreatedatabase("user.db", context.mode_private, null);
mhelper = userdbhelper.getinstance(this, 1);
mhelper.openwritelink();
}
@override
protected void onstop() {
super.onstop();
mhelper.closelink();
}
@override
public void onclick(view v) {
if (v.getid() == r.id.btn_save) {
string name = et_name.gettext().tostring();
string age = et_age.gettext().tostring();
string height = et_height.gettext().tostring();
string weight = et_weight.gettext().tostring();
if (name == null || name.length() <= 0) {
showtoast("请先填写姓名");
return;
}
if (age == null || age.length() <= 0) {
showtoast("请先填写年龄");
return;
}
if (height == null || height.length() <= 0) {
showtoast("请先填写身高");
return;
}
if (weight == null || weight.length() <= 0) {
showtoast("请先填写体重");
return;
}
userinfo info = new userinfo();
info.name = name;
info.age = integer.parseint(age);
info.height = long.parselong(height);
info.weight = float.parsefloat(weight);
info.married = married;
info.phone = phone;
info.password = password;
//info.update_time = dateutil.getcurdatestr("yyyy-mm-dd hh:mm:ss");
info.update_time = dateutil.getnowdate(dateutil.datepattern.all_time);
log.d(tag, "onclick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
mhelper.insert(info);
intent intent = new intent(infowriteactivity.this, inforeadactivity.class);
startactivity(intent);
showtoast("数据已写入sqlite数据库");
}
}
private void showtoast(string desc) {
toast.maketext(this, desc, toast.length_short).show();
}
public static void starthome(context mcontext) {
intent intent = new intent(mcontext, infowriteactivity.class);
mcontext.startactivity(intent);
}
}
inforeadactivity
package com.example.helloworld;
import androidx.appcompat.app.appcompatactivity;
import android.content.context;
import android.content.intent;
import android.content.sharedpreferences;
import android.database.sqlite.sqlitedatabase;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.edittext;
import android.widget.spinner;
import android.widget.toast;
public class infowriteactivity extends appcompatactivity implements view.onclicklistener {
private static final string tag = "huahua";
private userdbhelper mhelper;
private edittext et_name;
private edittext et_age;
private edittext et_height;
private edittext et_weight;
private boolean married = false;
private string phone;
private string password;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_shared_preferences);
et_name = findviewbyid(r.id.et_name);
et_age = findviewbyid(r.id.et_age);
et_height = findviewbyid(r.id.et_height);
et_weight = findviewbyid(r.id.et_weight);
findviewbyid(r.id.btn_save).setonclicklistener(this);
sharedpreferences sps = getsharedpreferences("login",context.mode_private);
sharedpreferences.editor editor = sps.edit();
phone = sps.getstring("phone","");
password = sps.getstring("password","");
arrayadapter<string> typeadapter = new arrayadapter<string>(this, r.layout.item_dropdown, typearray);
typeadapter.setdropdownviewresource(r.layout.item_dropdown);
spinner sp_married = findviewbyid(r.id.sp_married);
sp_married.setadapter(typeadapter);
sp_married.setprompt("请选择婚姻状况");
sp_married.setselection(0);
sp_married.setonitemselectedlistener(new typeselectedlistener());
}
private string[] typearray = {"未婚", "已婚"};
class typeselectedlistener implements adapterview.onitemselectedlistener {
public void onitemselected(adapterview<?> arg0, view arg1, int arg2, long arg3) {
married = (arg2 == 0) ? false : true;
}
public void onnothingselected(adapterview<?> arg0) {
}
}
@override
protected void onstart() {
super.onstart();
sqlitedatabase mdb = getapplicationcontext().openorcreatedatabase("user.db", context.mode_private, null);
mhelper = userdbhelper.getinstance(this, 1);
mhelper.openwritelink();
}
@override
protected void onstop() {
super.onstop();
mhelper.closelink();
}
@override
public void onclick(view v) {
if (v.getid() == r.id.btn_save) {
string name = et_name.gettext().tostring();
string age = et_age.gettext().tostring();
string height = et_height.gettext().tostring();
string weight = et_weight.gettext().tostring();
if (name == null || name.length() <= 0) {
showtoast("请先填写姓名");
return;
}
if (age == null || age.length() <= 0) {
showtoast("请先填写年龄");
return;
}
if (height == null || height.length() <= 0) {
showtoast("请先填写身高");
return;
}
if (weight == null || weight.length() <= 0) {
showtoast("请先填写体重");
return;
}
userinfo info = new userinfo();
info.name = name;
info.age = integer.parseint(age);
info.height = long.parselong(height);
info.weight = float.parsefloat(weight);
info.married = married;
info.phone = phone;
info.password = password;
info.update_time = dateutil.getnowdate(dateutil.datepattern.all_time);
log.d(tag, "onclick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
mhelper.insert(info);
intent intent = new intent(infowriteactivity.this, inforeadactivity.class);
startactivity(intent);
showtoast("数据已写入sqlite数据库");
}
}
private void showtoast(string desc) {
toast.maketext(this, desc, toast.length_short).show();
}
public static void starthome(context mcontext) {
intent intent = new intent(mcontext, infowriteactivity.class);
mcontext.startactivity(intent);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。