本文实例为大家分享了Android SharedPreferences实现记住密码和自动登录,供大家参考,具体内容如下
效果图:
第一次进入进来
勾选记住密码和自动登录成功后,第二次进来
说明:中间存在的图片或者多余的其他部分可删掉。留下最主要的填写部分和登陆按钮即可。功能还是可以实现的。
XML文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout 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:orientation = "vertical" android:background = "@drawable/bj" tools:context = "com.example.application.MainActivity" > < ImageView android:layout_marginTop = "50dp" android:layout_width = "100dp" android:layout_height = "100dp" android:src = "@drawable/login_tx_1" android:layout_gravity = "center" /> < FrameLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" > < EditText android:layout_width = "300dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:focusable = "true" android:focusableInTouchMode = "true" android:clickable = "true" android:hint = "请输入账号" android:gravity = "center" android:paddingRight = "100dp" android:id = "@+id/login_uname" /> < TextView android:layout_width = "38dp" android:layout_height = "33dp" android:layout_marginLeft = "30dp" android:padding = "6dp" android:gravity = "center" android:drawableLeft = "@drawable/uname" /> </ FrameLayout > < FrameLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" > < EditText android:layout_width = "300dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:focusable = "true" android:focusableInTouchMode = "true" android:clickable = "true" android:hint = "请输入密码" android:gravity = "center" android:paddingRight = "100dp" android:password = "true" android:id = "@+id/login_upass" /> < TextView android:layout_width = "38dp" android:layout_height = "33dp" android:layout_marginLeft = "30dp" android:padding = "6dp" android:gravity = "center" android:drawableLeft = "@drawable/upass" /> </ FrameLayout > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" > < CheckBox android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "记住密码" android:id = "@+id/login_auto" /> < CheckBox android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginLeft = "50dp" android:text = "自动登录" android:id = "@+id/login_btn" /> </ LinearLayout > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:gravity = "center" android:layout_margin = "15dp" > < ImageButton android:layout_width = "40dp" android:layout_height = "40dp" android:src = "@drawable/login_qq" /> < ImageButton android:layout_width = "40dp" android:layout_height = "40dp" android:src = "@drawable/login_weixin" android:layout_marginLeft = "60dp" /> < ImageButton android:layout_width = "40dp" android:layout_height = "40dp" android:src = "@drawable/login_xinlan" android:layout_marginLeft = "60dp" android:id = "@+id/login_xinlan" /> </ LinearLayout > < LinearLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < Button android:layout_width = "240dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:background = "@drawable/a_radio_button_selector_1" android:gravity = "center" android:text = "登录" android:id = "@+id/login_login" /> < Button android:layout_width = "240dp" android:layout_height = "wrap_content" android:layout_gravity = "center" android:layout_marginTop = "20dp" android:background = "@drawable/a_radio_button_selector_1" android:gravity = "center" android:text = "忘记密码" android:id = "@+id/login_find" /> < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center" android:layout_marginTop = "20dp" android:text = "没有账号,立即注册" android:textColor = "#6efafa" android:textSize = "15dp" android:onClick = "JumpRegister" /> </ LinearLayout > </ LinearLayout > |
Java文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
package com.example.application; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; import com.sun.util.DBHelper; import java.util.HashMap; import cn.sharesdk.framework.Platform; import cn.sharesdk.framework.PlatformActionListener; import cn.sharesdk.framework.ShareSDK; import cn.sharesdk.sina.weibo.SinaWeibo; public class MainActivity extends AppCompatActivity { public static String LoginUid= null ; public static String LoginName= null ; private Button login_login; private Button login_find; private EditText login_uname; private EditText login_upass; private CheckBox login_auto; private CheckBox login_btn; private SharedPreferences sp; private ImageButton login_xinlan; private Platform weibo; private DBHelper dbHelper; private SQLiteDatabase sqLiteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); this .requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //获取控件 login_uname = (EditText) findViewById(R.id.login_uname); login_upass = (EditText) findViewById(R.id.login_upass); login_auto = (CheckBox) findViewById(R.id.login_auto); //记住密码 login_btn = (CheckBox) findViewById(R.id.login_btn); //自动登录 login_login = (Button) findViewById(R.id.login_login); //登录 login_find = (Button) findViewById(R.id.login_find); login_xinlan = (ImageButton) findViewById(R.id.login_xinlan); //调用数据库 dbHelper = new DBHelper( this , "dtb.db" , null , 1 ); sqLiteDatabase = dbHelper.getWritableDatabase(); //第三方登录 weibo = ShareSDK.getPlatform(SinaWeibo.NAME); login_xinlan.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //回调信息,可以在这里获取基本的授权返回的信息,但是注意如果做提示和UI操作要传到主线程handler里去执行 weibo.setPlatformActionListener( new PlatformActionListener () { @Override public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) { String openid = platform.getDb().getUserId(); String nickname = platform.getDb().getUserName(); Cursor cursor=sqLiteDatabase.rawQuery( "select * from dtb_users where uname=?" , new String[]{openid}); LoginUid=cursor.getString(cursor.getColumnIndex( "uid" )); if (cursor.moveToNext()){ Log.i( "test" , "已经注册过!" ); } else { sqLiteDatabase.execSQL( "insert into dtb_users(uname,upass,name,levelnumber) values('" +openid+ "','null','" +nickname+ "','" + 1 + "')" ); } //跳转 MainActivity.LoginName=openid; Intent intent= new Intent(MainActivity. this ,MainMianActivity. class ); startActivity(intent); } @Override public void onError(Platform arg0, int arg1, Throwable arg2) { // TODO Auto-generated method stub arg2.printStackTrace(); } @Override public void onCancel(Platform arg0, int arg1) { // TODO Auto-generated method stub } }); //authorize与showUser单独调用一个即可 weibo.authorize(); //单独授权,OnComplete返回的hashmap是空的 weibo.showUser( null ); //授权并获取用户信息 //移除授权 // weibo.removeAccount(true); } }); //自动登录判断 sp = this .getSharedPreferences( "userInfo" , 0 ); String name=sp.getString( "USER_NAME" , "" ); String pass =sp.getString( "PASSWORD" , "" ); boolean choseRemember =sp.getBoolean( "remember" , false ); boolean choseAutoLogin =sp.getBoolean( "autologin" , false ); //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码 if (choseRemember){ login_uname.setText(name); login_upass.setText(pass); login_auto.setChecked( true ); } //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录 if (choseAutoLogin){ login_btn.setChecked( true ); Cursor cursor= sqLiteDatabase.rawQuery( "select * from dtb_users where uname=? and upass=?" , new String[]{name,pass}); if (cursor.moveToNext()){ new LoginThread().start(); LoginName=name; LoginUid=cursor.getString(cursor.getColumnIndex( "uid" )); } } // 登录监听事件 现在默认为用户名为:admin 密码:123 login_login.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String userName=login_uname.getText().toString(); String userPass=login_upass.getText().toString(); SharedPreferences.Editor editor =sp.edit(); Cursor cursor= sqLiteDatabase.rawQuery( "select * from dtb_users where uname=? and upass=?" , new String[]{userName,userPass}); if (cursor.moveToNext()){ //判断是否查询到此数据 Toast.makeText(MainActivity. this , "登录成功" , Toast.LENGTH_SHORT).show(); LoginName=userName; LoginUid=cursor.getString(cursor.getColumnIndex( "uid" )); //是否记住密码 //记住用户名、密码、 editor.putString( "USER_NAME" , userName); editor.putString( "PASSWORD" ,userPass); if (login_auto.isChecked()){ editor.putBoolean( "remember" , true ); } else { editor.putBoolean( "remember" , false ); } //是否自动登录 if (login_btn.isChecked()){ editor.putBoolean( "autologin" , true ); } else { editor.putBoolean( "autologin" , false ); } editor.commit(); //跳转界面 Intent intent = new Intent(MainActivity. this ,MainMianActivity. class ); startActivity(intent); Toast.makeText(MainActivity. this , "登录成功!" , Toast.LENGTH_SHORT).show(); // finish(); } else { Toast.makeText(MainActivity. this , "用户名或密码错误,请重新登录" , Toast.LENGTH_LONG).show(); } } }); } public void JumpRegister(View view){ Intent intent= new Intent( this ,RegisterActivity. class ); startActivity(intent); finish(); } //子线程 控制自动睡眠2秒钟后自动登录 class LoginThread extends Thread{ @Override public void run() { try { sleep( 2000 ); Intent intent = new Intent(MainActivity. this ,MainMianActivity. class ); startActivity(intent); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
Demo下载:记住密码和自动登录
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/a985548426/article/details/74852504