zoukankan      html  css  js  c++  java
  • 3.4 存储简单数据的利器——Preferences

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.sample3_6"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.sample3_6.MyActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="25dip"
            android:id="@+id/TextView01"
            
            />                                               <!-- 添加TextView -->
    </LinearLayout>
    package com.example.sample3_6;
    
    import java.security.acl.LastOwnerException;
    import java.util.Date;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextView;
    
    public class MyActivity extends Activity {
    
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            SharedPreferences sp = this.getSharedPreferences("sharePre", Context.MODE_PRIVATE);
            String lastLogin = sp.getString(
                    "ll",        //键值
                    null       //默认值
            );
         if(lastLogin ==null){
             lastLogin = "欢迎您,您是第一次访问本Preferences"; 
         }else{
            lastLogin =  "欢迎回来,您上次于" + lastLogin + "登录";
         }
         SharedPreferences.Editor editor = sp.edit();
         editor.putString("ll", new Date().toLocaleString());
         editor.commit();
         tv = (TextView) this.findViewById(R.id.TextView01);
         tv.setText(lastLogin);
         
    /*    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    */
    }
    }
  • 相关阅读:
    vue 如何点击按钮返回上一页
    vue遍历数组和对象的方法以及他们之间的区别
    css隐藏滚动条
    DOM编程以及domReady加载的几种方式
    修改默认滚动条默认样式
    面试题集锦
    正则表达式
    闭包及应用以及顺序处理ajax请求
    实现自己的(模仿jquery)toggle函数
    Asp.Net与SEO Viewstate优化终极解决方案
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7462620.html
Copyright © 2011-2022 走看看