SharedPreferences 简单记录(13)

//SharedPreferences操做的是键值对的数据,操做当前应用程序的存储空间,
//保存位置 data/data/{当前应用包名}/share_pref/文件名.xml
public class MainActivity extends Activity {
	private TextView textview;
	private float fontSize;
	private int fontColor;
	private int bgColor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textview = (TextView) this.findViewById(R.id.textview);
		read(null);// 启动时读取退出时保存的状态
		registerForContextMenu(textview);// 注册一个上下文菜单TextView
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// 上下文菜单
		getMenuInflater().inflate(R.menu.menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	// 上下文菜单监听
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.font_max:
			fontSize += 5; // 每点一次上下文菜单字体加5
			textview.setTextSize(fontSize);
			break;
		case R.id.font_sub:
			fontSize -= 5;// 每点一次上下文菜单字体减5
			textview.setTextSize(fontSize);
			break;
		case R.id.font_color:
			// rgb三色:颜色自动随机
			fontColor = Color.rgb((int) (Math.random() * 255),
					(int) (Math.random() * 255), (int) (Math.random() * 255));
			textview.setTextColor(fontColor);
			break;
		case R.id.font_bgColor:
			// rgb三色:颜色自动随机
			bgColor = Color.rgb((int) (Math.random() * 255),
					(int) (Math.random() * 255), (int) (Math.random() * 255));
			textview.setBackgroundColor(bgColor);
			break;
		}
		return super.onContextItemSelected(item);
	}
    
	public void wirte(View view) {
		SharedPreferences preferences = getSharedPreferences("myStyle",
				Context.MODE_PRIVATE);// 参数含义:文件名,和文件读写模式
		SharedPreferences.Editor editor = preferences.edit();
		editor.putFloat("fontSize", fontSize);
		editor.putInt("fontColor", fontColor);
		editor.putInt("bgColor", bgColor);

		// 没添加上下文菜单,只有按钮时的操做
		// editor.putFloat("fontSize", 12);
		// editor.putInt("fontColor", Color.CYAN);
		// editor.putInt("bgColor", Color.BLUE);

		editor.commit();
		// 提交 :保存位置 data/data/{当前应用包名}/share_pref/mystyle.xml
	}

	public void read(View view) {
		// 从上面保存的文件中读取出数据
		SharedPreferences preferences = getSharedPreferences("myStyle",
				Context.MODE_PRIVATE);// 参数含义:文件名,和文件读写模式
		fontSize = preferences.getFloat("fontSize", 25);
		fontColor = preferences.getInt("fontColor", Color.GREEN);
		bgColor = preferences.getInt("bgColor", Color.LTGRAY);
		textview.setTextSize(fontSize);
		textview.setTextColor(fontColor);
		textview.setBackgroundColor(bgColor);

		// 没添加上下文菜单,只有按钮时的操做
		// fontSize = preferences.getFloat("fontSize", 25);
		// fontColor = preferences.getInt("fontColor", Color.GREEN); bgColor =
		// preferences.getInt("bgColor", Color.BLACK);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		wirte(null);// 退出时保存当前的状态
	}
}
//主布局文件
<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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/bnt_wirte"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="wirte"
        android:text="写数据" />

    <Button
        android:id="@+id/bnt_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="read"
        android:text="读数据" />

</LinearLayout>

//上下文菜单布局menu.xml放在res/menu文件夹下
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/font_max"
        android:title="字体放大"/>
    <item
        android:id="@+id/font_sub"
        android:title="字体缩小"/>
    <item
        android:id="@+id/font_color"
        android:title="字体颜色"/>
    <item
        android:id="@+id/font_bgColor"
        android:title="背景颜色"/>

</menu>
相关文章
相关标签/搜索