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

Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能

 更新時(shí)間:2019年05月27日 14:41:02   作者:Vivinia_Vivinia  
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android SharedPreferences保存登錄數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下

目標(biāo)效果: 

程序運(yùn)行顯示一個(gè)登陸框,用戶名輸入admin,密碼輸入123456會(huì)提示登錄成功,如果不是則提示不正確,如果勾選保存用戶名,在下一個(gè)程序打開時(shí),用戶名會(huì)自動(dòng)讀取并顯示。

1.activity_main.xml頁(yè)面存放所有的控件,我在每一行都使用了線性布局。

activity_main.xml頁(yè)面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_marginLeft="20dp"
 android:layout_marginRight="20dp"
 tools:context=".SecondActivity" >
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:orientation="horizontal" >
 
  <TextView
   android:id="@+id/tvName"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="用戶名:" />
 
  <EditText
   android:id="@+id/etInputName"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="2" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="60dp"
  android:orientation="horizontal" >
 
  <TextView
   android:id="@+id/tvPass"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="密 碼:" />
 
  <EditText
   android:id="@+id/etInputPass"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="2" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="100dp"
  android:orientation="horizontal" >
 
  <CheckBox
   android:id="@+id/cbSave"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:checked="false"
   android:text="保存用戶名" />
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="130dp"
  android:orientation="horizontal" >
 
  <Button
   android:id="@+id/btLogin"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="登錄" />
 
  <Button
   android:id="@+id/btCancel"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:text="取消" />
 </LinearLayout>
 
</RelativeLayout>

2.MainActivity.java頁(yè)面處理登錄和保存數(shù)據(jù)。

MainActivity.java頁(yè)面:

package com.example.sharedpreferences;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener{
 
 SharedPreferences pref;
 Editor editor;
 private EditText etInputName,etInputPass;
 private CheckBox ckSave;
 private Button btLogin,btCancel;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 /**
 * 獲取控件id
 */
 getId();
 /**
 * 保存數(shù)據(jù)
 */
 saveData();
 /**
 * 綁定點(diǎn)擊事件
 */
 bindClick();
 }
 
 
 
 /**
 * 獲取控件id
 */
 private void getId() {
 etInputName=(EditText) findViewById(R.id.etInputName);
 etInputPass=(EditText) findViewById(R.id.etInputPass);
 ckSave=(CheckBox) findViewById(R.id.cbSave);
 btLogin=(Button) findViewById(R.id.btLogin);
 btCancel=(Button) findViewById(R.id.btCancel);
 }
 /**
 * 保存數(shù)據(jù)
 */
 private void saveData() {
 
 //獲得SharedPreferences對(duì)象
 pref=getSharedPreferences("userInfo",MODE_PRIVATE);//將內(nèi)容存放到名為userInfo的文檔內(nèi)
 
 //獲得SharedPreferences.Editor對(duì)象
 editor=pref.edit();
 
 String name=pref.getString("userName","");//獲取用戶名
 if(name.equals("")){//如果name為空,代表未選擇保存用戶名
 ckSave.setChecked(false);//不勾選
 }else{
 ckSave.setChecked(true);
 etInputName.setText(name);//將讀取到的name值賦值到EditText中
 }
 }
 
 /**
 * 綁定點(diǎn)擊事件
 */
 private void bindClick() {
 btLogin.setOnClickListener(this);
 btCancel.setOnClickListener(this);
 }
 
 /**
 * 按鈕點(diǎn)擊事件
 */
 @Override
 public void onClick(View view) {
 switch (view.getId()) {
 case R.id.btLogin:
 String name=etInputName.getText().toString().trim();//獲取輸入的名字并去掉空格
 String pass=etInputPass.getText().toString().trim();//獲取輸入的密碼并去掉空格
 if("admin".equals(name)&&"123456".equals(pass)){
 if(ckSave.isChecked()){//如果選擇保存用戶名
  editor.putString("userName",name);
  editor.commit();//提交數(shù)據(jù)
 }else{//如果未選擇保存用戶名
  editor.remove("userName");//刪除用戶名
  editor.commit();//提交數(shù)據(jù)(每次更改都需要提交)
 }
 Toast.makeText(SecondActivity.this,"登錄成功",Toast.LENGTH_SHORT).show();
 }else{
 Toast.makeText(SecondActivity.this,"用戶名或密碼不正確",Toast.LENGTH_SHORT).show();
 }
 break;
 case R.id.btCancel:
 break;
 }
 }
}

3.保存的文件目錄可以查看的到,點(diǎn)擊右上角進(jìn)入,找到data->data->當(dāng)前目錄的包名->shared-prefs->新建的文件名

4.另外,點(diǎn)擊右上角導(dǎo)出可以暫時(shí)保存到桌面,然后選擇打開方式可以查看里邊信息。

5.還有一點(diǎn)是,當(dāng)程序在真機(jī)上運(yùn)行時(shí),file explore打不開data文件夾,根據(jù)網(wǎng)上的經(jīng)驗(yàn),真機(jī)先root,然后在手機(jī)上裝上R.E 管理器(或類似軟件),將/data/data的權(quán)限修改為可讀可寫可執(zhí)行,然后,就可以在eclipse中展開了。


6.SharedPreferences多用于配置信息或者內(nèi)容較少的數(shù)據(jù)的保存,當(dāng)數(shù)據(jù)量復(fù)雜或者較大,還是需要使用數(shù)據(jù)庫(kù)。

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

相關(guān)文章

最新評(píng)論