• SharedPreferences存储


    SharedPreferences存储

    通过健值的方式存储数据,方便获取

    layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/save_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Save data"
            />
    
        <Button
            android:id="@+id/restore_data"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Restore data"
            />
    
    </LinearLayout>
    
    

    main

    package demo.jq.com.sharedpreferencestest;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    /**
     * @author Jim
     */
    public class MainActivity extends AppCompatActivity {
        private static final String TAG = "MainActivity";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button saveData = (Button) findViewById(R.id.save_data);
            Button restoreData = (Button) findViewById(R.id.restore_data);
            saveData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                    editor.putString("name","Jim");
                    editor.putInt("age",27);
                    editor.putBoolean("married",true);
                    editor.apply();
                }
            });
    
            restoreData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
                    String name = pref.getString("name","");
                    int age = pref.getInt("age",0);
                    boolean married = pref.getBoolean("married",false);
                    Log.d(TAG,"name is " + name);
                    Log.d(TAG,"age is " + age);
                    Log.d(TAG,"married is " + married);
                }
            });
    
        }
    }
    
    

  • 相关阅读:
    【leetcode】590. N-ary Tree Postorder Traversal
    【leetcode】589. N-ary Tree Preorder Traversal
    【leetcode】402. Remove K Digits
    【leetcode】42. Trapping Rain Water
    【leetcode】32. Longest Valid Parentheses
    【leetcode】870. Advantage Shuffle
    【leetcode】22. Generate Parentheses
    BEC translation exercise 2
    New Concept English three (50)
    BEC translation exercise 1
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/7693504.html
Copyright © 2020-2023  润新知