SharedPreferences(手机自带内存)读和写基本操做android
效果图示例:app
一、activity_mian.xml布局文件dom
代码编辑器
<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"
android:orientation="vertical" >ide
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />布局
<Button
android:id="@+id/bt_writer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写数据"
android:onClick="writer"/>字体
<Button
android:id="@+id/bt_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读数据"
android:onClick="read"/>this
</LinearLayout>xml
-----------------------------事件
二、menu文件下的布局set_activity.xml布局文件
代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/font_add"
android:title="加大字体"/>
<item
android:id="@+id/font_sub"
android:title="减少字体"/>
<item
android:id="@+id/font_color"
android:title="字体颜色"/>
<item
android:id="@+id/bg_color"
android:title="背景颜色"/>
</menu>
==============================
三、MianActivity 类
代码
public class MainActivity extends Activity {
private TextView textview; private float font_size; private int font_color; private int bg_color; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.textview = (TextView) this.findViewById(R.id.textview); //一打开app 就读 写好的数据 read(null); //注册textview registerForContextMenu(textview); } // 建立一个上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.set_menu, menu); } //重写上写文菜单 点击 事件 监听 @Override public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.font_add: font_size+=5; textview.setTextSize(font_size); break; case R.id.font_sub: font_size-=5; textview.setTextSize(font_size); break; case R.id.font_color: font_color = Color.rgb((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)); textview.setTextColor(font_color); break; case R.id.bg_color: bg_color = Color.rgb((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255)); textview.setBackgroundColor(bg_color); break; } return super.onContextItemSelected(item); } //写数据 public void writer(View view){ //第一个参数 -- 要把数据 写入到的文件名 //第二个参数 -- 写入数据的时候 是什么 模式 //数据保存在 data/data/包名 SharedPreferences preferences = getSharedPreferences("my_data", Context.MODE_PRIVATE); //得到 编辑器 SharedPreferences.Editor editor = preferences.edit(); //用得到的编辑器 写入数据 editor.putFloat("fontsize", font_size); editor.putInt("fontcolor", font_color); editor.putInt("bgcolor", bg_color); //提交 editor.commit(); } //读数据 public void read(View view){ SharedPreferences preferences = getSharedPreferences("my_data", Context.MODE_PRIVATE); //读取 数据 font_size = preferences.getFloat("fontsize", 60); font_color = preferences.getInt("fontcolor", Color.BLUE); bg_color = preferences.getInt("bgcolor", Color.WHITE); textview.setTextSize(font_size); textview.setTextColor(font_color); textview.setBackgroundColor(bg_color); } //一按退出 就保存当前的数据 @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); //一按退出 就保存当前的数据 writer(null); }}