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

首頁 > 系統 > Android > 正文

Android SharedPreferences實現記住密碼和自動登錄界面

2019-12-12 03:37:15
字體:
來源:轉載
供稿:網友

SharedPreferences介紹:

SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置參數,它是采用xml文件存放數據的,文件存放在"/data/data<package name>/shared_prefs"目錄下。

SharedPreferences的用法:

由于SharedPreferences是一個接口,而且在這個接口里沒有提供寫入數據和讀取數據的能力。但它是通過其Editor接口中的一些方法來操作SharedPreference的,用法見下面代碼:

Context.getSharedPreferences(String name,int mode)來得到一個SharedPreferences實例

name:是指文件名稱,不需要加后綴.xml,系統會自動為我們添加上。

mode:是指定讀寫方式,其值有三種,分別為:

Context.MODE_PRIVATE:指定該SharedPreferences數據只能被本應用程序讀、寫

Context.MODE_WORLD_READABLE:指定該SharedPreferences數據能被其他應用程序讀,但不能寫

Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數據能被其他應用程序讀寫。

結果截圖:



工程目錄:

Java代碼

LoginActivity.java

package com.liu.activity;  import android.app.Activity; import android.app.backup.SharedPreferencesBackupHelper; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.Spannable; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast;  public class LoginActivity extends Activity {   private EditText userName, password;  private CheckBox rem_pw, auto_login;  private Button btn_login;  private ImageButton btnQuit;  private String userNameValue,passwordValue;  private SharedPreferences sp;   public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);    //去除標題  this.requestWindowFeature(Window.FEATURE_NO_TITLE);  setContentView(R.layout.login);    //獲得實例對象  sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);  userName = (EditText) findViewById(R.id.et_zh);  password = (EditText) findViewById(R.id.et_mima);  rem_pw = (CheckBox) findViewById(R.id.cb_mima);  auto_login = (CheckBox) findViewById(R.id.cb_auto);  btn_login = (Button) findViewById(R.id.btn_login);  btnQuit = (ImageButton)findViewById(R.id.img_btn);      //判斷記住密碼多選框的狀態  if(sp.getBoolean("ISCHECK", false))  {   //設置默認是記錄密碼狀態   rem_pw.setChecked(true);   userName.setText(sp.getString("USER_NAME", ""));   password.setText(sp.getString("PASSWORD", ""));   //判斷自動登陸多選框狀態   if(sp.getBoolean("AUTO_ISCHECK", false))   {    //設置默認是自動登錄狀態    auto_login.setChecked(true);   //跳轉界面   Intent intent = new Intent(LoginActivity.this,LogoActivity.class);   LoginActivity.this.startActivity(intent);      }  }    // 登錄監聽事件 現在默認為用戶名為:liu 密碼:123  btn_login.setOnClickListener(new OnClickListener() {    public void onClick(View v) {   userNameValue = userName.getText().toString();   passwordValue = password.getText().toString();      if(userNameValue.equals("liu")&&passwordValue.equals("123"))   {    Toast.makeText(LoginActivity.this,"登錄成功", Toast.LENGTH_SHORT).show();    //登錄成功和記住密碼框為選中狀態才保存用戶信息    if(rem_pw.isChecked())    {    //記住用戶名、密碼、    Editor editor = sp.edit();    editor.putString("USER_NAME", userNameValue);    editor.putString("PASSWORD",passwordValue);    editor.commit();    }    //跳轉界面    Intent intent = new Intent(LoginActivity.this,LogoActivity.class);    LoginActivity.this.startActivity(intent);    //finish();      }else{       Toast.makeText(LoginActivity.this,"用戶名或密碼錯誤,請重新登錄", Toast.LENGTH_LONG).show();   }      }  });   //監聽記住密碼多選框按鈕事件  rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {   if (rem_pw.isChecked()) {       System.out.println("記住密碼已選中");    sp.edit().putBoolean("ISCHECK", true).commit();      }else {       System.out.println("記住密碼沒有選中");    sp.edit().putBoolean("ISCHECK", false).commit();      }    }  });    //監聽自動登錄多選框事件  auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {   if (auto_login.isChecked()) {    System.out.println("自動登錄已選中");    sp.edit().putBoolean("AUTO_ISCHECK", true).commit();    } else {    System.out.println("自動登錄沒有選中");    sp.edit().putBoolean("AUTO_ISCHECK", false).commit();   }   }  });    btnQuit.setOnClickListener(new OnClickListener() {     @Override   public void onClick(View v) {   finish();   }  });   } }

LogoActivity.java

package com.liu.activity; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.opengl.ETC1; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar;  public class LogoActivity extends Activity {  private ProgressBar progressBar;  private Button backButton;   protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  // 去除標題  this.requestWindowFeature(Window.FEATURE_NO_TITLE);  setContentView(R.layout.logo);   progressBar = (ProgressBar) findViewById(R.id.pgBar);  backButton = (Button) findViewById(R.id.btn_back);   Intent intent = new Intent(this, WelcomeAvtivity.class);  LogoActivity.this.startActivity(intent);   backButton.setOnClickListener(new OnClickListener() {    @Override   public void onClick(View v) {   finish();    }  });   }  } 

WelcomeActivity.java

package com.liu.activity;import android.app.Activity; import android.os.Bundle;  public class WelcomeAvtivity extends Activity {   @Override  protected void onCreate(Bundle savedInstanceState) {  // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.welcome);  }  }

布局文件:

login.xml文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:background="@drawable/logo_bg"  android:orientation="vertical" >   <RelativeLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content" >  <ImageButton   android:id="@+id/img_btn"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_alignParentRight="true"   android:background="@drawable/quit"/>   <TextView   android:id="@+id/tv_zh"   android:layout_width="wrap_content"   android:layout_height="35dip"   android:layout_marginLeft="12dip"   android:layout_marginTop="10dip"   android:gravity="bottom"   android:text="帳號:"   android:textColor="#000000"   android:textSize="18sp" />   <EditText   android:id="@+id/et_zh"   android:layout_width="fill_parent"   android:layout_height="40dip"   android:layout_below="@id/tv_zh"   android:layout_marginLeft="12dip"   android:layout_marginRight="10dip" />   <TextView   android:id="@+id/tv_mima"   android:layout_width="wrap_content"   android:layout_height="35dip"   android:layout_below="@id/et_zh"   android:layout_marginLeft="12dip"   android:layout_marginTop="10dip"   android:gravity="bottom"   android:text="密碼:"   android:textColor="#000000"   android:textSize="18sp" />   <EditText   android:id="@+id/et_mima"   android:layout_width="fill_parent"   android:layout_height="40dip"   android:layout_below="@id/tv_mima"   android:layout_marginLeft="12dip"   android:layout_marginRight="10dip"   android:maxLines="200"   android:password="true"   android:scrollHorizontally="true" />   <CheckBox   android:id="@+id/cb_mima"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_below="@id/et_mima"   android:layout_marginLeft="12dip"   android:text="記住密碼"   android:textColor="#000000" />   <CheckBox   android:id="@+id/cb_auto"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_below="@id/cb_mima"   android:layout_marginLeft="12dip"   android:text="自動登錄"   android:textColor="#000000" />  <Button   android:id="@+id/btn_login"   android:layout_width="80dip"   android:layout_height="40dip"   android:layout_below="@id/et_mima"   android:layout_alignParentRight="true"   android:layout_alignTop="@id/cb_auto"   android:layout_marginRight="10dip"   android:gravity="center"   android:text="登錄"   android:textColor="#000000"   android:textSize="18sp"/>     </RelativeLayout>    </LinearLayout> 

logo.xml文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:background="@drawable/logo_bg"  android:orientation="vertical" >   <RelativeLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_weight="3">   <ProgressBar   android:id="@+id/pgBar"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_centerInParent="true" />   <TextView   android:id="@+id/tv1"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_below="@id/pgBar"   android:layout_centerHorizontal="true"   android:text="正在登錄..."   android:textColor="#000000"   android:textSize="18sp" />  </RelativeLayout>   <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_weight="1"  android:gravity="center"  android:orientation="vertical" >   <Button   android:id="@+id/btn_back"   android:layout_width="70dip"   android:layout_height="35dip"   android:text="取消"   android:textColor="#000000"   android:textSize="12sp" />  </LinearLayout>   </LinearLayout> 

welcome.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:layout_gravity="center"  android:background="@drawable/login_bg"  android:orientation="vertical" >   <TextView  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:gravity="center"  android:text="登陸成功,進入用戶界面"  android:textColor="#000000"  android:textSize="20sp" />  </LinearLayout>

工程下載連接:android-SharedPreferences_jb51.rar

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐乡市| 象山县| 军事| 扶余县| 沙坪坝区| 遂川县| 泽州县| 瑞安市| 云南省| 高青县| 琼海市| 邻水| 从化市| 南宁市| 沙坪坝区| 罗源县| 葫芦岛市| 常熟市| 将乐县| 牟定县| 牡丹江市| 凌云县| 灵武市| 太仆寺旗| 东乡族自治县| 合川市| 宽城| 沈阳市| 苗栗县| 吴江市| 密山市| 峡江县| 兰考县| 汶川县| 香港| 遂昌县| 湄潭县| 宜阳县| 当雄县| 文昌市| 成武县|