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>
相關文章
相關標籤/搜索