最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼

 更新時(shí)間:2021年04月26日 10:42:52   作者:今晚看星星  
這篇文章主要介紹了Android 使用 SharedPreferences 保存少量數(shù)據(jù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1 SharedPreferences 介紹

SharedPreferences是使用鍵值對(duì)的方式來存儲(chǔ)數(shù)據(jù)的

SharedPreferences share = getSharedPreferences("my_file", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = share.edit();
// 4 保存數(shù)據(jù)到文件
editor.putString("account", input_account.getText().toString());
editor.putString("password", input_password.getText().toString());
editor.putBoolean("pass_remem", pass_remem.isChecked());   // 單選框 選中時(shí)返回為 true

當(dāng)保存一條數(shù)據(jù)的時(shí)候,需要給這條數(shù)據(jù)提供一個(gè)對(duì)應(yīng)的鍵,可以通過這個(gè)把相應(yīng)的值取出來

SharedPreferences sharedPreferences = getSharedPreferences("my_file", Context.MODE_PRIVATE);
Boolean pass_remem_ = sharedPreferences.getBoolean("pass_remem", false);

它是一個(gè)輕量級(jí)的存儲(chǔ)類,特別適合用于保存軟件配置參數(shù)。使用SharedPreferences保存數(shù)據(jù),文件存放在/data/data/<package name>/shared_prefs目錄下

1.1 SharedPreferences 四種操作模式

  • Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容
  • Context.MODE_APPEND:模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
  • MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取.
  • MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入.

1.3 使用方法

由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力。但其內(nèi)部有一個(gè)Editor內(nèi)部接口,Editor接口有一系列方法來操作SharedPreference

1.edit( ) 獲得SharedPreferences.Edit對(duì)象 getSharedPreferences("myfile",0).edit( )

2.向?qū)ο笾刑砑訑?shù)據(jù)

  • putString( )
  • putInt( )
  • putBoolean( )
editor.putString(“name”, “張三");
editor.putInt(“age”, 21);
editor.putBoolean("married",true)

3.commit( ) 提交數(shù)據(jù),完成數(shù)據(jù)存儲(chǔ)操作 editor.commit( );

4.從文件中讀取數(shù)據(jù) 第一個(gè)參數(shù)為KEY 第二個(gè)參數(shù)為訪問失敗時(shí)的默認(rèn)值

  • getString( )
  • getInt( )
  • getBoolean( )
getString ("name", "");
getInt (“age", 0);
getBoolean (“married", false);

2 使用 SharedPreferences 進(jìn)行登錄

2.1 前端設(shè)計(jì)

在這里插入圖片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textSize="36sp"
        android:gravity="center"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="10dp"
         />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/S_account"
            android:gravity="center"
            android:textSize="16sp"
            />

        <EditText
            android:id="@+id/input_account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:ems="10"
            android:textSize="16sp"
            android:paddingLeft="10dp"
            android:inputType="textPersonName"
            android:hint="@string/S_input_account"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/S_password"
            android:gravity="center"
            android:textSize="16sp"
            />

        <EditText
            android:id="@+id/input_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:ems="10"
            android:textSize="16sp"
            android:paddingLeft="10dp"
            android:inputType="numberPassword"
            android:hint="@string/S_input_password"/>
    </LinearLayout>

    <CheckBox
        android:id="@+id/password_remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_gravity="right"
        android:layout_marginRight="30dp"
        android:layout_marginBottom="33dp"
        android:text="@string/S_pass_remem"
        android:checked="false"
        />
    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="20dp"
        android:text="@string/S_button_submit"
        android:textSize="24sp"

        />
</LinearLayout>

2.1 Control層

public class MainActivity extends AppCompatActivity {

    private EditText input_account, input_password;
    private CheckBox pass_remem;
    private Button submit_button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 1 獲取各個(gè)組件的信息, 并存儲(chǔ)到數(shù)據(jù)層
        input_account = this.findViewById(R.id.input_account);
        input_password = this.findViewById(R.id.input_password);
        pass_remem = this.findViewById(R.id.password_remember);
        submit_button = this.findViewById(R.id.submit);

        // 2 設(shè)置按鈕的點(diǎn)擊事件
        submit_button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 3 獲取SharedPreferences
                SharedPreferences share = getSharedPreferences("my_file", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = share.edit();
                // 4 保存數(shù)據(jù)到文件
                editor.putString("account", input_account.getText().toString());
                editor.putString("password", input_password.getText().toString());
                editor.putBoolean("pass_remem", pass_remem.isChecked());   // 單選框 選中時(shí)返回為 true

                // 5 提交數(shù)據(jù), 并進(jìn)行提示
                editor.commit();
                Toast.makeText(MainActivity.this, "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show();

                // App條狀
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);

            }
        });

        // 6 如果選中,下一次加載數(shù)據(jù)
        SharedPreferences sharedPreferences = getSharedPreferences("my_file", Context.MODE_PRIVATE);
        Boolean pass_remem_ = sharedPreferences.getBoolean("pass_remem", false);

        if (pass_remem_) {

            String account = sharedPreferences.getString("account", "");
            String password = sharedPreferences.getString("password", "");


            input_account.setText(account);
            input_password.setText(password);
            pass_remem.setChecked(pass_remem_);  // 恢復(fù)到原來的狀態(tài)

        }

    }
}

到此這篇關(guān)于Android 使用 SharedPreferences 保存少量數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Android 保存數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android實(shí)現(xiàn)可點(diǎn)擊展開的TextView

    Android實(shí)現(xiàn)可點(diǎn)擊展開的TextView

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可點(diǎn)擊展開的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Android項(xiàng)目基本結(jié)構(gòu)詳解

    Android項(xiàng)目基本結(jié)構(gòu)詳解

    這篇文章主要為大家詳細(xì)介紹了Android項(xiàng)目基本結(jié)構(gòu),從最基本的內(nèi)容講起,帶你逐步進(jìn)入用C#進(jìn)行Android應(yīng)用開發(fā)的樂園,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android 應(yīng)用中插入廣告的實(shí)例

    Android 應(yīng)用中插入廣告的實(shí)例

    本文主要介紹Android應(yīng)用中插入廣告,這里提供了詳細(xì)的資料及實(shí)現(xiàn)示例代碼,有興趣的小伙伴可以參考下
    2016-08-08
  • Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法

    Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法

    本篇文章主要介紹了Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-04-04
  • Android截取視頻幀并轉(zhuǎn)化為Bitmap示例

    Android截取視頻幀并轉(zhuǎn)化為Bitmap示例

    利用MediaMetadataRetriever按照時(shí)間截取視頻并轉(zhuǎn)換為Bitmap存放于SDCard,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android開發(fā)之App widget用法實(shí)例分析

    Android開發(fā)之App widget用法實(shí)例分析

    這篇文章主要介紹了Android開發(fā)之App widget用法,結(jié)合實(shí)例形式詳細(xì)分析了Android開發(fā)中使用App widget組件的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Flutter構(gòu)建自定義Widgets的全過程記錄

    Flutter構(gòu)建自定義Widgets的全過程記錄

    在Flutter實(shí)際開發(fā)中,大家可能會(huì)遇到flutter框架中提供的widget達(dá)不到我們想要的效果,這時(shí)就需要我們?nèi)プ远xwidget,下面這篇文章主要給大家介紹了關(guān)于Flutter構(gòu)建自定義Widgets的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • 橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析

    橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析

    這篇文章主要為大家介紹了橫豎屏切換導(dǎo)致頁面頻繁重啟screenLayout解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • ListView異步加載圖片實(shí)現(xiàn)思路(優(yōu)化篇)

    ListView異步加載圖片實(shí)現(xiàn)思路(優(yōu)化篇)

    關(guān)于listview的異步加載,網(wǎng)上其實(shí)很多示例了,中心思想都差不多,不過很多版本或是有bug,或是有性能問題有待優(yōu)化,下面就讓在下闡述其原理以探索個(gè)中奧秘
    2013-04-04
  • 解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功)

    解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功)

    這篇文章主要介紹了解決Android studio3.6安裝后gradle Download失敗(構(gòu)建不成功),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03

最新評(píng)論

鹿泉市| 西畴县| 波密县| 澄迈县| 大同市| 巴马| 福清市| 和政县| 荣昌县| 黎平县| 贵港市| 旬邑县| 宁城县| 静海县| 长阳| 佳木斯市| 柞水县| 沙雅县| 南乐县| 河南省| 吉水县| 广河县| 大埔区| 绍兴县| 正宁县| 垣曲县| 绥阳县| 安达市| 尼勒克县| 资源县| 枣强县| 牡丹江市| 开江县| 寻甸| 昌黎县| 浠水县| 天津市| 汾阳市| 元氏县| 稻城县| 来宾市|