【Android開発チュートリアル】プリセット情報の取得

8038 ワード

この章は『Beginning-Android-4-Application-Development』から翻訳されていますが、翻訳が適切でないところがあれば、ご指摘ください.
原書購入住所http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
SharedPreferencesオブジェクトを使用してアプリケーションのデータを保存する方法を示します.保存したアプリケーションデータを特殊なActivityで修正することもできます.
1.新しいプロジェクト、UsingPreferencesを作成します.
2.resフォルダの下に新しいフォルダ、xmlを作成します.新しいフォルダにファイルを新規作成します.xml.
3.myappppreferences.xmlファイルにコードを記述します.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Category 1">
        <CheckBoxPreference
            android:title="Checkbox"
            android:defaultValue="false"
            android:summary="True or False"
            android:key="checkboxPref" />
        </PreferenceCategory>                        
    <PreferenceCategory android:title="Category 2">
        <EditTextPreference
            android:summary="Enter a string"
            android:defaultValue="[Enter a string here]"
            android:title="Edit Text"
            android:key="editTextPref" 
            />            
        <RingtonePreference
            android:summary="Select a ringtone"
            android:title="Ringtones"
            android:key="ringtonePref" 
            />            
        <PreferenceScreen                
            android:title="Second Preference Screen"
            android:summary=
                "Click here to go to the second Preference Screen"
            android:key="secondPrefScreenPref" >                            
            <EditTextPreference
                android:summary="Enter a string"
                android:title="Edit Text (second Screen)"
                android:key="secondEditTextPref"
                />                
        </PreferenceScreen>        
    </PreferenceCategory> 
</PreferenceScreen>
4. パッケージパスの下にクラスを新規作成し、AppPreferenceActivityを作成します.
5.AppPreferenceActivityのコード.
public class AppPreferenceActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        PreferenceManager prefMgr = getPreferenceManager();
        prefMgr.setSharedPreferencesName("appPreferences");

        //---load the preferences from an XML file---
        addPreferencesFromResource(R.xml.myapppreferences);
    }
}

6. AndroidManifestでxmlにAppPreferenceActivityクラスのエントリを追加します.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.manoel.UsingPreferences"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".UsingPreferencesActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".AppPreferenceActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action 
                    android:name="net.manoel.AppPreferenceActivity" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>            
    </application>

</manifest>
7. main.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"
    android:orientation="vertical" >

	<Button
		android:id="@+id/btnPreferences"
		android:text="Load Preferences Screen"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" 
		android:onClick="onClickLoad"/>

	<Button
		android:id="@+id/btnDisplayValues"
		android:text="Display Preferences Values"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" 
		android:onClick="onClickDisplay"/>

	<EditText  
		android:id="@+id/txtString"  
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" />

	<Button
		android:id="@+id/btnModifyValues"
		android:text="Modify Preferences Values"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:onClick="onClickModify"/>    

</LinearLayout>
8. UsingPreferencesActivity.JAvaにコードを追加します.
public class UsingPreferencesActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void onClickLoad(View view) {
		Intent i = new Intent("net.manoel.AppPreferenceActivity");
		startActivity(i);
	}
}
9. F 11を押してシミュレータの上でテストします.一番上のボタンをクリックして、UsingPreferencesというインタフェースにジャンプします.
【Android 开发教程】获取预设信息
10.CheckBoxをクリックして、その状態をcheckedとuncheckedの間で切り替えます.CATEGORY 1とCATEGORY 2を観察した.Edit Textをクリックし、いくつかの文字を入力し、OKを押してダイアログボックスを消します.
【Android 开发教程】获取预设信息
11.Ringtonesをクリックし、デフォルトのベルまたはミュートを選択します.本体でテストすると、もっと選択肢があるかもしれません.
【Android 开发教程】获取预设信息
12.Second Preference Screenをクリックすると、次の画面にジャンプします.
【Android 开发教程】获取预设信息
13.前の画面に戻りたい場合は、Backキーを押します.
14.オプションのいずれかを変更したら、/data/data/net.manoel.UsingPreferences/shared_prefsフォルダの下に新しいファイルが作成されます.DDMSで確認できます.
15.このファイルを開くと、似たような内容が見えます.