欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面

 更新時(shí)間:2017年02月24日 15:39:16   作者:liuyiming_  
本篇文章主要介紹了Android記住密碼和自動(dòng)登錄界面的實(shí)現(xiàn)(SharedPreferences),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

SharedPreferences介紹:

SharedPreferences是Android平臺(tái)上一個(gè)輕量級的存儲(chǔ)類,主要是保存一些常用的配置參數(shù),它是采用xml文件存放數(shù)據(jù)的,文件存放在"/data/data<package name>/shared_prefs"目錄下。

SharedPreferences的用法:

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

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

name:是指文件名稱,不需要加后綴.xml,系統(tǒng)會(huì)自動(dòng)為我們添加上。

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

Context.MODE_PRIVATE:指定該SharedPreferences數(shù)據(jù)只能被本應(yīng)用程序讀、寫

Context.MODE_WORLD_READABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀,但不能寫

Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀寫。

結(jié)果截圖:



工程目錄:

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); 
  
 //去除標(biāo)題 
 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
 setContentView(R.layout.login); 
  
 //獲得實(shí)例對象 
 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); 
  
  
 //判斷記住密碼多選框的狀態(tài) 
 if(sp.getBoolean("ISCHECK", false)) 
 { 
  //設(shè)置默認(rèn)是記錄密碼狀態(tài) 
  rem_pw.setChecked(true); 
  userName.setText(sp.getString("USER_NAME", "")); 
  password.setText(sp.getString("PASSWORD", "")); 
  //判斷自動(dòng)登陸多選框狀態(tài) 
  if(sp.getBoolean("AUTO_ISCHECK", false)) 
  { 
   //設(shè)置默認(rèn)是自動(dòng)登錄狀態(tài) 
   auto_login.setChecked(true); 
  //跳轉(zhuǎn)界面 
  Intent intent = new Intent(LoginActivity.this,LogoActivity.class); 
  LoginActivity.this.startActivity(intent); 
   
  } 
 } 
  
 // 登錄監(jiān)聽事件 現(xiàn)在默認(rèn)為用戶名為: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(); 
   //登錄成功和記住密碼框?yàn)檫x中狀態(tài)才保存用戶信息 
   if(rem_pw.isChecked()) 
   { 
   //記住用戶名、密碼、 
   Editor editor = sp.edit(); 
   editor.putString("USER_NAME", userNameValue); 
   editor.putString("PASSWORD",passwordValue); 
   editor.commit(); 
   } 
   //跳轉(zhuǎn)界面 
   Intent intent = new Intent(LoginActivity.this,LogoActivity.class); 
   LoginActivity.this.startActivity(intent); 
   //finish(); 
   
  }else{ 
   
   Toast.makeText(LoginActivity.this,"用戶名或密碼錯(cuò)誤,請重新登錄", Toast.LENGTH_LONG).show(); 
  } 
   
  } 
 }); 
 
 //監(jiān)聽記住密碼多選框按鈕事件 
 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(); 
   
  } 
 
  } 
 }); 
  
 //監(jiān)聽自動(dòng)登錄多選框事件 
 auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
  public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { 
  if (auto_login.isChecked()) { 
   System.out.println("自動(dòng)登錄已選中"); 
   sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); 
 
  } else { 
   System.out.println("自動(dòng)登錄沒有選中"); 
   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); 
 // 去除標(biāo)題 
 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="帳號(hào):" 
  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="自動(dòng)登錄" 
  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="登陸成功,進(jìn)入用戶界面" 
 android:textColor="#000000" 
 android:textSize="20sp" /> 
 
</LinearLayout>

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論