「Android/PreferenceFragmentCompat」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→参考) |
|||
行5: | 行5: | ||
==PreferenceFragmentを追加== | ==PreferenceFragmentを追加== | ||
+ | |||
+ | build.gradleに以下を追加 | ||
+ | <pre> | ||
+ | dependencies { | ||
+ | implementation 'com.android.support:appcompat-v7:28.0.0' | ||
+ | implementation 'com.android.support:preference-v7:28.0.0' | ||
+ | } | ||
+ | </pre> | ||
MainActivity.java | MainActivity.java | ||
行28: | 行36: | ||
</pre> | </pre> | ||
− | + | ui/main/PrefsFragment.java | |
<pre> | <pre> | ||
− | + | ||
− | + | import android.content.SharedPreferences; | |
− | + | import android.os.Bundle; | |
+ | import android.preference.EditTextPreference; | ||
+ | import android.preference.ListPreference; | ||
+ | import android.preference.Preference; | ||
+ | import android.support.v7.preference.PreferenceFragmentCompat; | ||
+ | |||
+ | import com.kaji.fragmentmyapplication.R; | ||
+ | |||
+ | public class PrefsFragment extends PreferenceFragmentCompat { | ||
+ | |||
+ | private static String TAG = "NativeAdSampleActivity"; | ||
+ | private EditTextPreference zoneIdPref; | ||
+ | public static String extraZoneId = "extraZoneId"; | ||
+ | |||
+ | public static PrefsFragment newInstance() { | ||
+ | return new PrefsFragment(); | ||
+ | } | ||
+ | @Override | ||
+ | public void onCreatePreferences(Bundle bundle, String s) { | ||
+ | setPreferencesFromResource(R.xml.preferences, s); | ||
+ | } | ||
} | } | ||
+ | </pre> | ||
+ | |||
+ | res/layout/preferences.xml | ||
+ | <pre> | ||
+ | <?xml version="1.0" encoding="utf-8"?> | ||
+ | <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | ||
+ | <SwitchPreferenceCompat | ||
+ | android:defaultValue="false" | ||
+ | android:key="red" | ||
+ | android:summary="This is red." | ||
+ | android:title="red" /> | ||
+ | </PreferenceScreen> | ||
</pre> | </pre> | ||
2018年12月27日 (木) 13:19時点における版
Fragmentについて
Fragmentについて理解してない場合はこちら
android/Fragment [ショートカット]
PreferenceFragmentを追加
build.gradleに以下を追加
dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:preference-v7:28.0.0' }
MainActivity.java
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.kaji.fragmentmyapplication.ui.main.MainFragment; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .replace(R.id.container, MainFragment.newInstance()) .commitNow(); } } }
ui/main/PrefsFragment.java
import android.content.SharedPreferences; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.support.v7.preference.PreferenceFragmentCompat; import com.kaji.fragmentmyapplication.R; public class PrefsFragment extends PreferenceFragmentCompat { private static String TAG = "NativeAdSampleActivity"; private EditTextPreference zoneIdPref; public static String extraZoneId = "extraZoneId"; public static PrefsFragment newInstance() { return new PrefsFragment(); } @Override public void onCreatePreferences(Bundle bundle, String s) { setPreferencesFromResource(R.xml.preferences, s); } }
res/layout/preferences.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <SwitchPreferenceCompat android:defaultValue="false" android:key="red" android:summary="This is red." android:title="red" /> </PreferenceScreen>
参考
【Android】続・PreferenceFragmentCompatを使った設定画面(kotrin?) http://phicdy.hatenablog.com/entry/setting_ui_by_preference_fragment_compat
PreferenceActivity/PreferenceFragmentを使った設定画面 http://phicdy.hatenablog.com/entry/2015/09/08/%E3%80%90Android%E3%80%91PreferenceActivity/PreferenceFragment%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E8%A8%AD%E5%AE%9A%E7%94%BB%E9%9D%A2