安卓实现密码保存和自动登录.docx
- 文档编号:659356
- 上传时间:2023-04-29
- 格式:DOCX
- 页数:19
- 大小:466.46KB
安卓实现密码保存和自动登录.docx
《安卓实现密码保存和自动登录.docx》由会员分享,可在线阅读,更多相关《安卓实现密码保存和自动登录.docx(19页珍藏版)》请在冰点文库上搜索。
安卓实现密码保存和自动登录
xmlversion="1.0"encoding="utf-8"?
>
android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: background="@drawable/logo_bg" android: orientation="vertical"> android: layout_width="fill_parent" android: layout_height="wrap_content"> android: id="@+id/img_btn" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_alignParentRight="true" android: background="@drawable/quit"/> android: id="@+id/tv_zh" android: layout_width="wrap_content" android: layout_height="35dip" android: layout_marginLeft="12dip" android: layout_marginTop="10dip" android: gravity="bottom" android: text="帐号: " android: textColor="#000000" android: textSize="18sp"/> android: id="@+id/et_zh" android: layout_width="fill_parent" android: layout_height="40dip" android: layout_below="@id/tv_zh" android: layout_marginLeft="12dip" android: layout_marginRight="10dip"/> android: id="@+id/tv_mima" android: layout_width="wrap_content" android: layout_height="35dip" android: layout_below="@id/et_zh" android: layout_marginLeft="12dip" android: layout_marginTop="10dip" android: gravity="bottom" android: text="密码: " android: textColor="#000000" android: textSize="18sp"/> android: id="@+id/et_mima" android: layout_width="fill_parent" android: layout_height="40dip" android: layout_below="@id/tv_mima" android: layout_marginLeft="12dip" android: layout_marginRight="10dip" android: maxLines="200" android: password="true" android: scrollHorizontally="true"/> android: id="@+id/cb_mima" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_below="@id/et_mima" android: layout_marginLeft="12dip" android: text="记住密码" android: textColor="#000000"/> android: id="@+id/cb_auto" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_below="@id/cb_mima" android: layout_marginLeft="12dip" android: text="自动登录" android: textColor="#000000"/> 布局文件: logo.xml xmlversion="1.0"encoding="utf-8"? > android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: background="@drawable/logo_bg" android: orientation="vertical"> android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="3"> android: id="@+id/pgBar" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_centerInParent="true"/> android: id="@+id/tv1" android: layout_width="wrap_content" android: layout_height="wrap_content" android: layout_below="@id/pgBar" android: layout_centerHorizontal="true" android: text="正在登录..." android: textColor="#000000" android: textSize="18sp"/> android: layout_width="fill_parent" android: layout_height="wrap_content" android: layout_weight="1" android: gravity="center" android: orientation="vertical"> 布局文件: welcom.xml xmlversion="1.0"encoding="utf-8"? > android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: layout_gravity="center" android: background="@drawable/login_bg" android: orientation="vertical"> android: layout_width="fill_parent" android: layout_height="wrap_content" android: gravity="center" android: text="登陆成功,进入用户界面" android: textColor="#000000" android: textSize="20sp"/> java代码: LoginActivity.java packagecom.wang.activity; importandroid.app.Activity; importandroid.content.Context; importandroid.content.Intent; importandroid.content.SharedPreferences; importandroid.content.SharedPreferences.Editor; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.view.Window; importandroid.widget.Button; importandroid.widget.CheckBox; importandroid.widget.CompoundButton; importandroid.widget.CompoundButton.OnCheckedChangeListener; importandroid.widget.EditText; importandroid.widget.ImageButton; importandroid.widget.Toast; publicclassLoginActivityextendsActivity{ privateEditTextuserName,passWord; privateCheckBoxrem_pw,auto_login; privateButtonbtn_login; privateImageButtonbtnQuit; privateStringuserNameValue,passwordValue; privateSharedPreferencessp; privatefinalStringMAK="innoview"; publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //去掉TitleBar this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); //获取shareprefrence里面的数据 sp=this.getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE); userName=(EditText)findViewById(R.id.et_zh); passWord=(EditText)findViewById(R.id.et_mima); rem_pw=(CheckBox)findViewById(R.id.cb_mima); auto_login=(CheckBox)findViewById(R.id.cb_auto); btn_login=(Button)findViewById(R.id.btn_login); btnQuit=(ImageButton)findViewById(R.id.img_btn); //判断记住密码框状态 if(sp.getBoolean("ISCHECK",false)) { //记住密码框状态标记为选中 rem_pw.setChecked(true); try{ userNameValue=sp.getString("USER_NAME",""); System.out.println("<<<<<<<<<<<<"+"加密后的用户名"+userNameValue); Stringusername=AESEncryptor.decrypt(MAK,userNameValue); System.out.println("<<<<<<<<<<<<"+"解密后的用户名"+username); userName.setText(username); }catch(Exceptione){ Toast.makeText(LoginActivity.this,"用户名解密异常",Toast.LENGTH_SHORT).show(); e.printStackTrace(); //TochangebodyofcatchstatementuseFile|Settings|FileTemplates. } Stringpassword=null; try{ passwordValue=sp.getString("PASSWORD",""); System.out.println("<<<<<<<<<<<<"+"加密后的密码"+passwordValue); password=AESEncryptor.decrypt(MAK,passwordValue); System.out.println("<<<<<<<<<<<<"+"解密后的密码"+password); }catch(Exceptione){ Toast.makeText(LoginActivity.this,"密码解密异常",Toast.LENGTH_SHORT).show(); e.printStackTrace(); //TochangebodyofcatchstatementuseFile|Settings|FileTemplates. } passWord.setText(password); //判断自动登录框状态 if(sp.getBoolean("AUTO_ISCHECK",false)) { //自动登录框状态标记为选中 auto_login.setChecked(true); Intentintent=newIntent(LoginActivity.this,LogoActivity.class); LoginActivity.this.startActivity(intent); } } //用户名: login秘密: 123456 btn_login.setOnClickListener(newOnClickListener(){ publicvoidonClick(Viewv){ userNameValue=userName.getText().toString(); passwordValue=passWord.getText().toString(); if(userNameValue.equals("login")&&passwordValue.equals("123456")) { Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show(); //如果记住密码框未选中状态 if(rem_pw.isChecked()) { Editoreditor=sp.edit(); // AESEncryptor.encrypt(userNameValue); try{ editor.putString("USER_NAME",AESEncryptor.encrypt(MAK,userNameValue)); System.out.println("<<<<<<<<"+"加密后的用户名"+AESEncryptor.encrypt(MAK,userNameValue)); }catch(Exceptione){ Toast.makeText(LoginActivity.this,"用户名加密异常",Toast.LENGTH_SHORT).show(); e.printStackTrace(); //TochangebodyofcatchstatementuseFile|Settings|FileTemplates. } try{ editor.putString("PASSWORD",AESEncryptor.encrypt(MAK,passwordValue));
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实现 密码 保存 自动 登录