国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android:利用SharedPreferences實現自動登錄

2019-12-12 04:32:40
字體:
來源:轉載
供稿:網友

本文介紹了Android:利用SharedPreferences實現自動登錄,具體如下:

主要代碼:

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);    // 初始化用戶名、密碼、記住密碼、自動登錄、登錄按鈕  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();    //如果上次選了記住密碼,那進入登錄頁面也自動勾選記住密碼,并填上用戶名和密碼  if(choseRemember){   username.setText(name);   userpassword.setText(pass);   remember.setChecked(true);  }  //如果上次登錄選了自動登錄,那進入登錄頁面也自動勾選自動登錄  if(choseAutoLogin){   autologin.setChecked(true);  }        login.setOnClickListener(new OnClickListener() {     // 默認可登錄帳號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);         }                         //是否自動登錄      if(autologin.isChecked()){              editor.putBoolean("autologin", true);             }else{       editor.putBoolean("autologin", false);      }     editor.commit();           //跳轉     Intent intent =new Intent(LoginActivity.this,SuccessActivity.class);     startActivity(intent);    } else {     Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤,請重新登錄!",       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="自動登錄" /> <Button  android:id="@+id/login"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="登錄" /></LinearLayout>

源碼下載:源碼

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 炎陵县| 西安市| 永德县| 石景山区| 林周县| 南雄市| 新郑市| 三原县| 商都县| 高碑店市| 滕州市| 华安县| 合江县| 呈贡县| 华容县| 莱西市| 大同市| 九江县| 南充市| 五大连池市| 明水县| 宁津县| 玛纳斯县| 孝感市| 平遥县| 辛集市| 驻马店市| 洛扎县| 开远市| 青神县| 长垣县| 信丰县| 烟台市| 固原市| 伽师县| 明光市| 邢台县| 鄂尔多斯市| 丹江口市| 海丰县| 尼勒克县|