次のエントリ: PreferenceのEditTextとListは文字列だよ [Android]
PreferenceActivity/PreferenceScreenで設定画面(Checkbox, EditText, List)
2011-07-03-1 / カテゴリ: [Android] / [permlink]
画面は、Windows7(x64)+Eclipse3.6+ADT11環境
[2011-06-08-1]の続きみたいな感じで。
超基礎(笑)
新規作成で「Android XML ファイル」を選択し、

リソースタイプで「Preference」を選択する。
※ ファイル名は任意。Javaから参照するID名になる

ファイルができるとこんな感じ

作成直後のXMLはこんな感じ

以下、[2011-06-08-1]の設定のインタフェースを作る感じで。
手抜き見やすくするために表示用文字列をリソースじゃなくて直値指定してます。


Keyに getBoolean()/putBoolean() で使用した文字列を指定。
XMLは5-9行目部分

XMLは11-15行目部分

XMLは17-23行目部分。
外部参照してる項目設定は↓
res/values/arrays.xml
これで、たとえば"MOTOROLA XOOM"を選択すると、出力されるxmlファイルは
addPreferencesFromResource()で、表示対象のPreferenceScreenのxmlファイル名のリソースIDを指定する。
あとは通常のActivityと同じ容量でManifestへ記述を行い、startActity()で起動してあげれば良い。
左から順にチェックボックス・エディットテキスト・リストの表示。
エディットテキストとリストは、タップをすると入力用の Dialog が起動する。

PreferenceActivityを使って出力したPreferenceは、アプリグローバルのパッケージ名プリファレンスとして保存されるので、アプリ内からは PreferenceManager.getDefaultSharedPreferences(context) で参照/更新できる。
リストとエディットテキストが文字列な理由を別エントリへ移動
[2011-07-04-1]
[2011-06-08-1]の続きみたいな感じで。
超基礎(笑)
新規作成で「Android XML ファイル」を選択し、

リソースタイプで「Preference」を選択する。
※ ファイル名は任意。Javaから参照するID名になる

ファイルができるとこんな感じ

作成直後のXMLはこんな感じ
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> </PreferenceScreen>XMLエディタで開いて「追加」を押して、設定項目の追加を行う

以下、[2011-06-08-1]の設定のインタフェースを作る感じで。

設定画面のXML定義(PreferenceScreen)
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:summary="SSLを使用する" android:key="usessl" android:title="SSL" ></CheckBoxPreference> <EditTextPreference android:key="name" android:title="サイト名" android:summary="サイトの名称" ></EditTextPreference> <ListPreference android:key="list" android:entryValues="@array/listpref_value" android:entries="@array/listpref_entries" android:title="端末" android:summary="端末を選択" ></ListPreference> </PreferenceScreen>全体でこんな感じ
チェックボックス(boolean)
Boolean値みたいなon/offで使用する。
Keyに getBoolean()/putBoolean() で使用した文字列を指定。
XMLは5-9行目部分
エディットテキスト(String)
テキストや数値の直接入力。扱うのが数値でも、内部ではStringなので注意。[2011-07-04-1]
XMLは11-15行目部分
リスト(string-array)
複数項目の中からチェックボックスのI/Fで1個選択。項目は文字列じゃないとダメ[2011-07-04-1]なので、文字配列を指定する。
XMLは17-23行目部分。
外部参照してる項目設定は↓
res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="listpref_entries"> <item>HTC Desire</item> <item>MOTOROLA XOOM</item> <item>Nexus S</item> </string-array> <string-array name="listpref_value"> <item>0</item> <item>1</item> <item>2</item> </string-array> </resources>
これで、たとえば"MOTOROLA XOOM"を選択すると、出力されるxmlファイルは
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> : <string name="list">1</string> : </map>となる。
画面用のJava実装(PreferenceActivity)
package jp.jp_z.samplepreferenceapp; import android.os.Bundle; import android.preference.PreferenceActivity; public class MySettings extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.mysettings); } }基本はこれだけ、ActivityでなくPreferenceActivityを継承したクラスを作成する。
addPreferencesFromResource()で、表示対象のPreferenceScreenのxmlファイル名のリソースIDを指定する。
あとは通常のActivityと同じ容量でManifestへ記述を行い、startActity()で起動してあげれば良い。
左から順にチェックボックス・エディットテキスト・リストの表示。
エディットテキストとリストは、タップをすると入力用の Dialog が起動する。



PreferenceActivityを使って出力したPreferenceは、アプリグローバルのパッケージ名プリファレンスとして保存されるので、アプリ内からは PreferenceManager.getDefaultSharedPreferences(context) で参照/更新できる。
リストとエディットテキストが文字列な理由を別エントリへ移動
[2011-07-04-1]
Referrer (Inside):
[2011-07-25-1]
[
コメント ]
次のエントリ: PreferenceのEditTextとListは文字列だよ [Android]
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
最終更新時間: 2013-05-02 16:12