Android:利用SharedPreferences實(shí)現(xiàn)自動(dòng)登錄
本文介紹了Android:利用SharedPreferences實(shí)現(xiàn)自動(dòng)登錄,具體如下:
主要代碼:
public class LoginActivity extends Activity { private EditText username; private EditText userpassword; private CheckBox remember; private CheckBox autologin; private Button login; private SharedPreferences sp; private String userNameValue,passwordValue; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); // 初始化用戶名、密碼、記住密碼、自動(dòng)登錄、登錄按鈕 username = (EditText) findViewById(R.id.username); userpassword = (EditText) findViewById(R.id.userpassword); remember = (CheckBox) findViewById(R.id.remember); autologin = (CheckBox) findViewById(R.id.autologin); login = (Button) findViewById(R.id.login); sp = 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); // Toast.makeText(this, name, Toast.LENGTH_SHORT).show(); //如果上次選了記住密碼,那進(jìn)入登錄頁面也自動(dòng)勾選記住密碼,并填上用戶名和密碼 if(choseRemember){ username.setText(name); userpassword.setText(pass); remember.setChecked(true); } //如果上次登錄選了自動(dòng)登錄,那進(jìn)入登錄頁面也自動(dòng)勾選自動(dòng)登錄 if(choseAutoLogin){ autologin.setChecked(true); } login.setOnClickListener(new OnClickListener() { // 默認(rèn)可登錄帳號(hào)tinyphp,密碼123 @Override public void onClick(View arg0) { userNameValue = username.getText().toString(); passwordValue = userpassword.getText().toString(); SharedPreferences.Editor editor =sp.edit(); // TODO Auto-generated method stub if (userNameValue.equals("tinyphp") && passwordValue.equals("123")) { Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show(); //保存用戶名和密碼 editor.putString("USER_NAME", userNameValue); editor.putString("PASSWORD", passwordValue); //是否記住密碼 if(remember.isChecked()){ editor.putBoolean("remember", true); }else{ editor.putBoolean("remember", false); } //是否自動(dòng)登錄 if(autologin.isChecked()){ editor.putBoolean("autologin", true); }else{ editor.putBoolean("autologin", false); } editor.commit(); //跳轉(zhuǎn) Intent intent =new Intent(LoginActivity.this,SuccessActivity.class); startActivity(intent); } else { Toast.makeText(LoginActivity.this, "用戶名或密碼錯(cuò)誤,請(qǐng)重新登錄!", Toast.LENGTH_SHORT).show(); } } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" > </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="密碼:" /> <EditText android:id="@+id/userpassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" > </EditText> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="記住密碼" /> <CheckBox android:id="@+id/autologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自動(dòng)登錄" /> <Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登錄" /> </LinearLayout>
源碼下載:源碼
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中使用ExpandableListView 實(shí)現(xiàn)分組的實(shí)例
這篇文章主要介紹了Android 中使用ExpandableListView 實(shí)現(xiàn)分組的實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12Android獲取、更改包名的小技巧分享(超實(shí)用)
這篇文章主要給大家分享介紹了關(guān)于利用Android更改包名的一個(gè)小技巧,通過這個(gè)方法大家可以很方便的修改包名,再也不用為頻繁修改包名而感到頭疼,文中還給大家分享利一個(gè)android獲取手機(jī)所有應(yīng)用包名的方法,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解LeakCanary分析內(nèi)存泄露如何實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android中TextView實(shí)現(xiàn)垂直滾動(dòng)和上下滾動(dòng)效果
這篇文章主要介紹了Android中TextView實(shí)現(xiàn)垂直滾動(dòng)效和上下滾動(dòng)效果,需要的朋友可以參考下2017-04-04詳解Android使GridView橫向水平滾動(dòng)的實(shí)現(xiàn)方式
Android為我們提供了豎直方向的滾動(dòng)控件GridView,這篇文章主要介紹了Android使GridView橫向水平滾動(dòng)的實(shí)現(xiàn)方式,有興趣的可以了解一下2017-05-05Flutter加載圖片流程之ImageCache源碼示例解析
這篇文章主要為大家介紹了Flutter加載圖片流程之ImageCache源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android NDK中socket的用法以及注意事項(xiàng)分析
本篇文章是對(duì)Android NDK中socket的用法以及注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android webview與js的數(shù)據(jù)交互
有了WebView這個(gè)組件,Android應(yīng)用開發(fā)技術(shù)也就轉(zhuǎn)嫁到html與java數(shù)據(jù)交互上來。說白了就是js與WebView的數(shù)據(jù)交互,這就是本文所要討論的2017-04-04Android 繪制多級(jí)樹形選擇列表實(shí)例代碼
這篇文章主要介紹了Android 繪制多級(jí)樹形選擇列表的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-02-02