用户登录记住密码
来源:互联网 发布:淘宝拍卖书画是真的吗 编辑:程序博客网 时间:2024/05/01 09:02
当用户选中了记住密码复选框,并成功登录一次之后,这个时候如果再重新启动登录界
面,之前输入的用户名和密码就会显示在文本框中。涉及 SharedPreferences 的使用。
主要的页面实现代码
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bz0209.myapplication.MainActivity" android:background="#8ECFE5" > <EditText android:hint="请输入账号" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="54dp" android:layout_marginStart="54dp" android:layout_marginTop="33dp" android:id="@+id/textView2" /> <EditText android:text="请输入密码" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_alignLeft="@+id/textView2" android:layout_alignStart="@+id/textView2" android:layout_marginTop="10dp" android:id="@+id/textView3" /> <CheckBox android:text="记住密码" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:id="@+id/checkBox" android:layout_alignBottom="@+id/button" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/icon_user" android:layout_above="@+id/textView3" android:layout_toLeftOf="@+id/textView3" android:layout_toStartOf="@+id/textView3" android:id="@+id/imageView" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/icon_pass" android:layout_alignBottom="@+id/textView3" android:layout_toLeftOf="@+id/textView3" android:layout_toStartOf="@+id/textView3" android:id="@+id/imageView2" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/deer" android:id="@+id/imageView3" android:layout_below="@+id/button" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="15dp" android:layout_marginEnd="15dp" android:layout_marginTop="12dp" /> <Button android:text="登录" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:id="@+id/button" android:layout_below="@+id/textView3" android:layout_toRightOf="@+id/textView3" android:layout_marginLeft="47dp" android:layout_marginStart="47dp" android:layout_alignRight="@+id/imageView3" android:layout_alignEnd="@+id/imageView3" android:background="@drawable/btn" /></RelativeLayout>
内部的Java实现代码,主要用到SharedPreferences。
package com.example.bz0209.myapplication;import android.content.Intent;import android.content.SharedPreferences;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private EditText editText1; private EditText editText2; private Button button; private CheckBox checkBox; SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intView(); sharedPreferences=getSharedPreferences("login",MODE_PRIVATE); boolean isRem=sharedPreferences.getBoolean("checkBox",false); if(isRem){ String name=sharedPreferences.getString("name",""); String pass=sharedPreferences.getString("pass",""); editText1.setText(name); editText2.setText(pass); checkBox.isChecked(true); } } private void intView(){ editText1=(EditText) findViewById(R.id.textView2); editText2=(EditText) findViewById(R.id.textView3); button=(Button) findViewById(R.id.button); checkBox=(CheckBox) findViewById(R.id.checkBox); } private void login(){ String name=editText1.getText().toString(); String pass=editText2.getText().toString(); SharedPreferences.Editor editor=sharedPreferences.edit(); if(checkBox.isChecked()){ editor.putBoolean("checkBox",true); editor.putString("name",name); editor.putString("pass",pass); } else editor.clear(); editor.commit(); if("admin".equals(name)&&"pass".equals(pass)){ Intent intent=new Intent(this,welcome.class); startActivity(intent); finish(); } else Toast.makeText(this,"账号或密码错误",Toast.LENGTH_LONG).show(); }package com.example.bz0209.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class welcome extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); }}}
0 0
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录记住密码
- 用户登录(记住密码)
- 用户登录记住密码
- 一个Windows下的tail工具(即时查看日志)
- JS--addEventListener()
- VectorDrawable矢量图
- java 中的抽象方法
- 欢迎使用CSDN-markdown编辑器
- 用户登录记住密码
- c++风格字符串使用
- 红黑树
- HDFS创建快照备份数据
- MVC、MVP和MVVM的区别
- linux压缩与解压缩
- 【CODE[VS]】2822 爱在心中 强连通分量
- POJ_2299_Ultra-QuickSort & NYOJ_117_求逆序数
- GTX1050 Ti(notebook)+Ubuntu16.04上安装Cuda8.0