• SharedPreferences的简单使用


    package com.example.sharedpreferences;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        
        TextView tv;
        Button read,write;
        SharedPreferences preferences;
        SharedPreferences.Editor editor;
    
        
        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.tv);
            read = (Button)findViewById(R.id.read);
            write = (Button)findViewById(R.id.write);
            
            //声明preferences,通过getSharedPreferences方法得到,参数两个
            preferences = getSharedPreferences("test", MODE_WORLD_READABLE);
            
            //声明editor,通过preferences.edit()方法得到
            editor = preferences.edit();
            read.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    
                    //得到日期
                    String time = preferences.getString("time", null);
                    
                    //得到随机数
                    int randNum = preferences.getInt("random", 0);
                    String result = time == null ? "未写数据" : "the write time" + time
                            +"\n the randNum:" +randNum;
                    tv.setText(result);
                    
                    
                }
            });
            write.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日"
                    +"hh:mm:ss");
                    
                    //放入日期
                    editor.putString("time", sdf.format(new Date()));
                    //放入一个随机数
                    editor.putInt("random", (int)(Math.random()*100));
                    
                    //提交
                    editor.commit();
                    
                }
            });
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    package com.example.sharedpreferences;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        
        TextView tv;
        Button read,write;
        SharedPreferences preferences;
        SharedPreferences.Editor editor;
    
        
        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.tv);
            read = (Button)findViewById(R.id.read);
            write = (Button)findViewById(R.id.write);
            
            //声明preferences,通过getSharedPreferences方法得到,参数两个
            preferences = getSharedPreferences("test", MODE_WORLD_READABLE);
            
            //声明editor,通过preferences.edit()方法得到
            editor = preferences.edit();
            read.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    
                    //得到日期
                    String time = preferences.getString("time", null);
                    
                    //得到随机数
                    int randNum = preferences.getInt("random", 0);
                    String result = time == null ? "未写数据" : "the write time" + time
                            +"\n the randNum:" +randNum;
                    tv.setText(result);
                    
                    
                }
            });
            write.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日"
                    +"hh:mm:ss");
                    
                    //放入日期
                    editor.putString("time", sdf.format(new Date()));
                    //放入一个随机数
                    editor.putInt("random", (int)(Math.random()*100));
                    
                    //提交
                    editor.commit();
                    
                }
            });
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
    package com.example.sharedpreferences;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        
        TextView tv;
        Button read,write;
        SharedPreferences preferences;
        SharedPreferences.Editor editor;
    
        
        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.tv);
            read = (Button)findViewById(R.id.read);
            write = (Button)findViewById(R.id.write);
            
            //声明preferences,通过getSharedPreferences方法得到,参数两个
            preferences = getSharedPreferences("test", MODE_WORLD_READABLE);
            
            //声明editor,通过preferences.edit()方法得到
            editor = preferences.edit();
            read.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    
                    //得到日期
                    String time = preferences.getString("time", null);
                    
                    //得到随机数
                    int randNum = preferences.getInt("random", 0);
                    String result = time == null ? "未写数据" : "the write time" + time
                            +"\n the randNum:" +randNum;
                    tv.setText(result);
                    
                    
                }
            });
            write.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年mm月dd日"
                    +"hh:mm:ss");
                    
                    //放入日期
                    editor.putString("time", sdf.format(new Date()));
                    //放入一个随机数
                    editor.putInt("random", (int)(Math.random()*100));
                    
                    //提交
                    editor.commit();
                    
                }
            });
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
    }
  • 相关阅读:
    什么是 Spring 框架?Spring框架有哪些主要模块?
    java动态代理实现与原理详细分析
    react中 函数式组件hook的使用
    JavaScript中的delete的定义以及使用
    React中ref的使用
    使用react中antd design UI库from中的getFieldsValue,getFieldValue,validateFields,resetFields,getFieldDecorator,initialvalue的用法
    使用react和axios实现的城市三级联动
    关于react中antd design pro下面src/models
    关于thisState的那些事
    react的生命周期
  • 原文地址:https://www.cnblogs.com/merryjd/p/2845644.html
Copyright © 2020-2023  润新知