activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.appusecount;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text=(TextView) super.findViewById(R.id.count);
preferences=getSharedPreferences("count", MODE_WORLD_READABLE);
int useCount=preferences.getInt("count",0);
Toast.makeText(MainActivity.this, useCount+""+"哈哈", Toast.LENGTH_LONG).show();
editor=preferences.edit();
editor.putInt("count", ++useCount);
editor.commit();
text.setText("第"+useCount+"次使用");
}
}