Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
本文實(shí)例為大家分享了Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄,供大家參考,具體內(nèi)容如下
效果圖:
第一次進(jìn)入進(jìn)來(lái)
勾選記住密碼和自動(dòng)登錄成功后,第二次進(jìn)來(lái)
說(shuō)明:中間存在的圖片或者多余的其他部分可刪掉。留下最主要的填寫(xiě)部分和登陸按鈕即可。功能還是可以實(shí)現(xiàn)的。
XML文件
<?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="請(qǐng)輸入賬號(hào)" 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="請(qǐng)輸入密碼" 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="自動(dòng)登錄" 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="沒(méi)有賬號(hào),立即注冊(cè)" android:textColor="#6efafa" android:textSize="15dp" android:onClick="JumpRegister" /> </LinearLayout> </LinearLayout>
Java文件
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); //自動(dòng)登錄 login_login = (Button) findViewById(R.id.login_login); //登錄 login_find = (Button) findViewById(R.id.login_find); login_xinlan = (ImageButton) findViewById(R.id.login_xinlan); //調(diào)用數(shù)據(jù)庫(kù) 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) { //回調(diào)信息,可以在這里獲取基本的授權(quán)返回的信息,但是注意如果做提示和UI操作要傳到主線(xiàn)程handler里去執(zhí)行 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","已經(jīng)注冊(cè)過(guò)!"); }else{ sqLiteDatabase.execSQL("insert into dtb_users(uname,upass,name,levelnumber) values('"+openid+"','null','"+nickname+"','"+1+"')"); } //跳轉(zhuǎn) 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單獨(dú)調(diào)用一個(gè)即可 weibo.authorize();//單獨(dú)授權(quán),OnComplete返回的hashmap是空的 weibo.showUser(null);//授權(quán)并獲取用戶(hù)信息 //移除授權(quán) // weibo.removeAccount(true); } }); //自動(dòng)登錄判斷 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); //如果上次選了記住密碼,那進(jìn)入登錄頁(yè)面也自動(dòng)勾選記住密碼,并填上用戶(hù)名和密碼 if(choseRemember){ login_uname.setText(name); login_upass.setText(pass); login_auto.setChecked(true); } //如果上次登錄選了自動(dòng)登錄,那進(jìn)入登錄頁(yè)面也自動(dòng)勾選自動(dòng)登錄 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")); } } // 登錄監(jiān)聽(tīng)事件 現(xiàn)在默認(rèn)為用戶(hù)名為: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()){//判斷是否查詢(xún)到此數(shù)據(jù) Toast.makeText(MainActivity.this,"登錄成功", Toast.LENGTH_SHORT).show(); LoginName=userName; LoginUid=cursor.getString(cursor.getColumnIndex("uid")); //是否記住密碼 //記住用戶(hù)名、密碼、 editor.putString("USER_NAME", userName); editor.putString("PASSWORD",userPass); if(login_auto.isChecked()){ editor.putBoolean("remember", true); }else{ editor.putBoolean("remember", false); } //是否自動(dòng)登錄 if(login_btn.isChecked()){ editor.putBoolean("autologin", true); }else{ editor.putBoolean("autologin", false); } editor.commit(); //跳轉(zhuǎn)界面 Intent intent = new Intent(MainActivity.this,MainMianActivity.class); startActivity(intent); Toast.makeText(MainActivity.this, "登錄成功!", Toast.LENGTH_SHORT).show(); // finish(); }else{ Toast.makeText(MainActivity.this,"用戶(hù)名或密碼錯(cuò)誤,請(qǐng)重新登錄", Toast.LENGTH_LONG).show(); } } }); } public void JumpRegister(View view){ Intent intent=new Intent(this,RegisterActivity.class); startActivity(intent); finish(); } //子線(xiàn)程 控制自動(dòng)睡眠2秒鐘后自動(dòng)登錄 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下載:記住密碼和自動(dòng)登錄
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter通過(guò)Container實(shí)現(xiàn)時(shí)間軸效果
時(shí)間軸是前端UI經(jīng)常用到的效果,本文講解下Flutter如何通過(guò)Container實(shí)現(xiàn),感興趣的朋友可以了解下2021-05-05Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例
這篇文章主要介紹了Android 實(shí)現(xiàn)帶進(jìn)度條的WebView的實(shí)例的相關(guān)資料,這里介紹了Webview加載網(wǎng)頁(yè)的方法及帶進(jìn)度的Drawable文件view_progress_webview的實(shí)現(xiàn),需要的朋友可以參考下2017-07-07Android用戶(hù)注冊(cè)界面簡(jiǎn)單設(shè)計(jì)
這篇文章主要為大家分享了Android用戶(hù)注冊(cè)界面簡(jiǎn)單設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android自定義滾動(dòng)選擇器實(shí)例代碼
本篇文章主要介紹了Android自定義滾動(dòng)選擇器實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺(tái)訂單支付API設(shè)計(jì))
這篇文章主要介紹了Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺(tái)訂單支付API設(shè)計(jì))的相關(guān)資料,需要的朋友可以參考下2016-11-11Android應(yīng)用程序中讀寫(xiě)txt文本文件的基本方法講解
這篇文章主要介紹了Android應(yīng)用程序中讀寫(xiě)txt文本文件的基本方法講解,基本上依靠context.openFileInput()和context.openFileOutput()兩個(gè)方法為主,需要的朋友可以參考下2016-04-04flutter實(shí)現(xiàn)底部導(dǎo)航欄切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部導(dǎo)航欄切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)列表購(gòu)物車(chē)功能 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android實(shí)現(xiàn)氣泡動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)氣泡動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04Android 封裝Okhttp+Retrofit+RxJava,外加攔截器實(shí)例
下面小編就為大家分享一篇Android封裝Okhttp+Retrofit+RxJava,外加攔截器實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01