zoukankan      html  css  js  c++  java
  • Android [SharedPreference轻量级存储]

    SharedPreferencesActivity.java

    package com.xdw.a122.data;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    import com.xdw.a122.R;
    
    public class SharedPreferencesActivity extends AppCompatActivity {
        private EditText mEtName;
        private Button mBtnSave,mBtnShow;
        private TextView mTvContent;
        private SharedPreferences mSharedPreferences;
        private SharedPreferences.Editor mEditor;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shared_preferences);
            mEtName=findViewById(R.id.et_name);
            mBtnSave=findViewById(R.id.btn_save);
            mBtnShow=findViewById(R.id.btn_show);
            mTvContent=findViewById(R.id.tv_show);
    
            mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE);  //名称和类型
            mEditor=mSharedPreferences.edit();
    
            mBtnSave.setOnClickListener(new View.OnClickListener() {     //存储
                 @Override
                public void onClick(View v) {
                 mEditor.putString("name",mEtName.getText().toString());
                 //此处写要提交的内容
                    mEditor.apply();
                    //or commit(); 存储完成
                }
            });
            mBtnShow.setOnClickListener(new View.OnClickListener() {            //读取
                @Override
                public void onClick(View v) {
                    mTvContent.setText(mSharedPreferences.getString("name",""));
                }
            });
        }
    }

    activity_shared_preferences

    <?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=".data.SharedPreferencesActivity">
        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="输入内容"
            />
        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存"
            android:layout_marginTop="10dp"/>
        <Button
            android:id="@+id/btn_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示"/>
        <TextView
            android:id="@+id/tv_show"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"/>
    </LinearLayout>

    输入内容在 //存储 输入存储内容

    并在tv_show显示

    mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE);  //名称和类型
            mEditor=mSharedPreferences.edit();

    存储的类型

  • 相关阅读:
    【Beta】Scrum Meeting 8
    【Beta】Scrum Meeting 7
    【Beta】Scrum Meeting 6
    【Beta】Scrum Meeting 5
    【Beta】Scrum Meeting 4
    【Beta】Scrum Meeting 3
    “北航Clubs”项目汇报
    Alpha阶段展示报告
    Alpha阶段产品功能说明
    Alpha阶段个人贡献分
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10744952.html
Copyright © 2011-2022 走看看