• 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;
        }
    */
    }
    }
  • 相关阅读:
    MFC创建dc的总结
    mini2440驱动的静态加载
    linux文件IO的操作
    mini2440led驱动分析
    关于layout_gravity和gravity的区别
    UI界面和组件(二)
    UI界面和组件(一)
    关于Android的组件使用中出现的一些问题(一)
    关于Android api文档的一些问题
    梳理7---关于java中static方法一些记录
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7462620.html
Copyright © 2020-2023  润新知