搜尋此網誌

2012年5月21日 星期一

Android preference demo (Android 偏好設定)

Shared Preferences是用來儲存應用程式設定值的好用類別.

資料會一直存活在系統,直到應用程式被移除.

透過access preference name, 可以對資料做接收或者是寫入的動作.

範例如下.

========================
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cookbook.activity_lifecycle"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <activity android:name=".PreferenceDemo"
                  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>
PreferenceDemo.java

import com.cookbook.activity_lifecycle.R;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PreferenceDemo extends Activity{
    
    public static final String PREFS_NAME = "ap_settings";
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pref) ;
        final Button setbutton = (Button) findViewById(R.id.setButton);
        setbutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText et = (EditText)findViewById(R.id.editText1);
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("edit_text", et.getText().toString());
                // Commit the edits!
                editor.commit();
                et.setText("");
            }
        });
        
        final Button getButton = (Button) findViewById(R.id.getButton);
        getButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                EditText et = (EditText)findViewById(R.id.editText1);
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                et.setText(settings.getString("edit_text", null));
            }
        });
    }

}

pref.xml

<?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" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" 
            android:text="null"/>

        <Button
            android:id="@+id/setButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Set" />

        <Button
            android:id="@+id/getButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Get" />

    </LinearLayout>

Reference:Android Data Storage

沒有留言:

張貼留言