您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關怎么在Android中使用SharedPreferences實現記住密碼和自動登錄,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
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="請輸入賬號" 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文件
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(); } } } }
以上就是怎么在Android中使用SharedPreferences實現記住密碼和自動登錄,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。