15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android Studioで設定画面を作る

Last updated at Posted at 2014-11-26

(Android Studio 0.8.14) PreferenceActivityが作れますが、中身がほとんどDeprecatedってなんとかならんのかい!

  1. 専用のActivityを作る
  2. PreferenceFragmentを作る
  3. 動くようにしてやる

これでいってみます。

専用のActivityを作る

  • appを右クリックして、New → Activity → Blank Activityを作る
  • (ここではSettingsActivityと名付けます)
  • レイアウトを編集してFrameLayoutだけにする(コードは後で)
  • この1画面だけなので match_parentで、contextもここで指定しちゃいましょう
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/settings_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsActivity" />

PreferenceFragmentを作る

  • appを右クリックして、New → Fragment → Fragment(Blank)を作る
  • (ここではSettingsFragmentと名付けます)
  • Create layout XML?、include fragment factory method?、include interface callbacks? のチェックを外す
  • ソースを変更
  • 普通のFragmentみたいに再作成チェック(onCreateでsavedInstanceStateがNullか否か)しなくていいんだろか?
 public class SettingsFragment extends PreferenceFragment {
    public SettingsFragment() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

動くようにしてやる

  • FragmentにListenerを設定します
settingsFragment
    @Override
    public void onResume() {
        super.onResume();
        getPreferenceManager().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        getPreferenceManager().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
    }
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(getString(R.string.preference_key1))) {
            EditTextPreference pref = (EditTextPreference) findPreference(key);
            pref.setSummary(pref.getText());
        } else if (key.equals(getString(R.string.preference_key2))) {
            EditTextPreference pref = (EditTextPreference) findPreference(key);
            pref.setSummary(pref.getText());
        }
    }
  • Activityから呼んでやります
settingsActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if(savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                .replace(R.id.settings_container, new SettingsFragment())
                .commit();
        }
    }

最小限ではこのくらいで動くはずです。

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?