<?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; } */ } }