設定画面を簡単に作る
|Androidアプリケーションで設定情報を保存したい場合、Preferenceを使うと便利です。
以前TechBoosterでは「データを簡単に保存する方法」としてPreferenceを紹介しましたが、
Preferenceを使うことでデータの保存だけではなく、下の図のようなデータ入力のためのGUIも簡単に実装することが可能です。
今回はPreferenceを使った設定情報の入力・保存、取り出しまでの手順をご紹介します。
GUIの項目に関する情報はres/xml以下にxmlファイルとして記述します。
今回は例としてpreferences.xmlというファイルを作ります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <? xml version = "1.0" encoding = "utf-8" ?> < PreferenceScreen < CheckBoxPreference android:key = "checkbox_preference" android:title = "CheckBoxPreference" android:summary = "CheckBoxPreference" /> < EditTextPreference android:key = "edittext_preference" android:title = "EditTextPreference" android:summary = "EditTextPreference" android:dialogTitle = "EditTextPreference" /> < ListPreference android:key = "list_preference" android:title = "ListPreference" android:summary = "ListPreference" android:entries = "@array/entries_list_preference" android:entryValues = "@array/entryvalues_list_preference" android:dialogTitle = "ListPreference" /> < PreferenceScreen android:title = "Value Check" android:summary = "Move to Value Check Screen" > < intent android:action = "android.intent.action.VIEW" android:targetPackage = "org.jpn.techbooster.PreferenceSample" android:targetClass = "org.jpn.techbooster.PreferenceSample.ValueCheck" /> </ PreferenceScreen > </ PreferenceScreen > |
Preferenceは扱うデータの形式でいくつか種類があります。
今回は3つのPreferenceを使っています。
CheckBoxPreference
CheckBoxPreferenceはチェックされているかどうかという情報、すなわちBoolean値を扱うためのGUIです。
preferences.xmlには以下のように記述します。
1 2 3 4 | < CheckBoxPreference android:key = "checkbox_preference" android:title = "CheckBoxPreference" android:summary = "CheckBoxPreference" /> |
android:keyはこの値をほかの場所から参照したい場合に指定する識別子になりますので、ユニークな名前にする必要があります。
android:titleはGUI上で最も大きく表示されるラベルで、android:summaryはandroid:titleの下に表示される文字列です。
EditTextPreference
EditTextPreferenceはダイアログ上のテキストボックスに入力されたテキストデータを扱います。
preferences.xmlには以下のように記述します。
1 2 3 4 5 | < EditTextPreference android:key = "edittext_preference" android:title = "EditTextPreference" android:summary = "EditTextPreference" android:dialogTitle = "EditTextPreference" /> |
ここで新しくでてきたandroid:dialogTitleはその名のとおりダイアログ上のタイトルを指定します。
ListPreference
ListPreferenceはダイアログ上に表示されるリストの中からデータを選んでもらうGUIです。
preferences.xmlには以下のように記述します。
1 2 3 4 5 6 7 | < ListPreference android:key = "list_preference" android:title = "ListPreference" android:summary = "ListPreference" android:entries = "@array/entries_list_preference" android:entryValues = "@array/entryvalues_list_preference" android:dialogTitle = "ListPreference" /> |
android:entriesとandroid:entryValuesでリストで選択できる項目を指定します。
android:entriesはリストで表示される文字列、android:entryValuesが実際に設定される値になります。
これら情報はres/values/arrays.xmlに以下のように記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 | <? xml version = "1.0" encoding = "utf-8" ?> < resources > < string-array name = "entries_list_preference" > < item >Donut 1.6</ item > < item >Eclair 2.1</ item > < item >Froyo 2.2</ item > </ string-array > < string-array name = "entryvalues_list_preference" > < item >donut</ item > < item >eclair</ item > < item >froyo</ item > </ string-array > </ resources > |
設定画面の実装
このpreference.xmlをaddPreferencesFromResource()メソッドで読み込むことで設定画面ができあがります。
1 2 3 4 5 6 7 8 9 10 11 12 13 | import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; public class PreferenceSample extends PreferenceActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } |
設定情報の取り出し
では別のアクティビティを作ってこの設定情報を取り出してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.widget.TextView; public class ValueCheck extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); TextView result_cb = (TextView)findViewById(R.id.result_cb); TextView result_et = (TextView)findViewById(R.id.result_et); TextView result_list = (TextView)findViewById(R.id.result_list); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences( this ); boolean checkbox_preference = sharedPreferences.getBoolean( "checkbox_preference" , false ); if (checkbox_preference) { result_cb.setText( "TRUE" ); } else { result_cb.setText( "FALSE" ); } String edittext_preference = sharedPreferences.getString( "edittext_preference" , "unknown" ); result_et.setText(edittext_preference); String list_preference = sharedPreferences.getString( "list_preference" , "unknown" ); result_list.setText(list_preference); } } |
まず、PreferenceManager.getDefaultSharedPreferencesでこのアプリケーションのSharedPreferenceインスタンスを取得します。
インスタンスを取得したらPreferenceごとに設定値を取得していくのですが、
対象のPreferenceの種類によって使用するメソッドが異なる点にだけ注意が必要です。