最終更新:
moonlight_aska 2011年07月23日(土) 18:27:03履歴
プリファレンスアクティビティを使って設定された設定情報は, 「/data/data/アプリケーションのパッケージ名/shared_prefs」の下に登録されている. ("設定画面を作成する"の項を参照)
この設定情報の読み出しには, PreferenceManagerクラスを使用する.

この設定情報の読み出しには, PreferenceManagerクラスを使用する.
- PreferenceManager.getDefaultSharedPreferencesメソッドで, SharedPreferencesクラスのインスタンスを取得する.
- SharedPreferences.getBooleanメソッドにチェックボックスのキーを渡し, 値を取得する.
- SharedPreferences.getStringメソッドにエディットテキストのキーを渡し, 値を取得する.
- SharedPreferences.getStringメソッドにリストのキーを渡し, 値を取得する.
package com.moonlight_aska.android.preference01;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class Preference01 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView text = (TextView)findViewById(R.id.text_id);
String settingInfo = new String("設定情報\n");
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
// チェックボックスによる設定の内容
boolean checkbox = pref.getBoolean("checkbox_key", false);
settingInfo += "CheckBox : " + String.valueOf(checkbox) + "\n";
// エディットテキストによる設定の内容
String edittext = pref.getString("edittext_key", "なし");
settingInfo += "EditText : " + edittext + "\n";
// リストによる設定の内容
String list = pref.getString("list_key", "0");
settingInfo += "List : " + list;
text.setText(settingInfo);
}
}

コメントをかく