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

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

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

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

目標(biāo)效果: 

程序運行顯示一個登陸框,用戶名輸入admin,密碼輸入123456會提示登錄成功,如果不是則提示不正確,如果勾選保存用戶名,在下一個程序打開時,用戶名會自動讀取并顯示。

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

activity_main.xml頁面:

<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頁面處理登錄和保存數(shù)據(jù)。

MainActivity.java頁面:

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();
 /**
 * 綁定點擊事件
 */
 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對象
 pref=getSharedPreferences("userInfo",MODE_PRIVATE);//將內(nèi)容存放到名為userInfo的文檔內(nèi)
 
 //獲得SharedPreferences.Editor對象
 editor=pref.edit();
 
 String name=pref.getString("userName","");//獲取用戶名
 if(name.equals("")){//如果name為空,代表未選擇保存用戶名
 ckSave.setChecked(false);//不勾選
 }else{
 ckSave.setChecked(true);
 etInputName.setText(name);//將讀取到的name值賦值到EditText中
 }
 }
 
 /**
 * 綁定點擊事件
 */
 private void bindClick() {
 btLogin.setOnClickListener(this);
 btCancel.setOnClickListener(this);
 }
 
 /**
 * 按鈕點擊事件
 */
 @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.保存的文件目錄可以查看的到,點擊右上角進入,找到data->data->當(dāng)前目錄的包名->shared-prefs->新建的文件名

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

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


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

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

相關(guān)文章

  • Android仿微信加號菜單模式

    Android仿微信加號菜單模式

    這篇文章主要為大家詳細(xì)介紹了Android仿微信加號菜單模式的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Libgdx解決部分Android機型鎖屏崩潰的方法

    Libgdx解決部分Android機型鎖屏崩潰的方法

    今天小編就為大家分享一篇關(guān)于Libgdx解決部分Android機型鎖屏崩潰的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用示例

    Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用示例

    這篇文章主要介紹了Android編程仿Iphone拖動相片特效Gallery的簡單應(yīng)用,結(jié)合實例形式分析了Android圖形拖動特效的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android 顯示刷新頻率的實現(xiàn)代碼

    Android 顯示刷新頻率的實現(xiàn)代碼

    這篇文章主要介紹了Android 顯示刷新頻率的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Android圓角設(shè)置方法看著一篇文章就夠了

    Android圓角設(shè)置方法看著一篇文章就夠了

    我們在實際工作中,android經(jīng)常有需要實現(xiàn)圓角的場景,下面這篇文章主要給大家介紹了關(guān)于Android圓角設(shè)置方法的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì)需要的朋友可以參考下
    2023-02-02
  • Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實現(xiàn)方法

    Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實現(xiàn)方法

    這篇文章主要介紹了Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Mac Android Studio安裝圖文教程

    Mac Android Studio安裝圖文教程

    本文主要介紹了Mac Android Studio安裝教程,文中通過圖文代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Android Scroll滑動效果實例

    Android Scroll滑動效果實例

    這篇文章主要為大家分享了Android Scroll滑動效果實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android 三種動畫詳解及簡單實例

    Android 三種動畫詳解及簡單實例

    這篇文章主要介紹了Android 三種動畫詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android使用BottomTabBar實現(xiàn)底部導(dǎo)航頁效果

    Android使用BottomTabBar實現(xiàn)底部導(dǎo)航頁效果

    這篇文章主要介紹了Android使用BottomTabBar實現(xiàn)底部導(dǎo)航頁效果,本文通過實例代碼結(jié)合文字說明的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2018-03-03

最新評論