「Android/PreferenceFragmentCompat」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→参考) |
(→inconvertible types; cannnot cast Preferenceエラーが出る場合) |
||
行78: | 行78: | ||
==inconvertible types; cannnot cast Preferenceエラーが出る場合== | ==inconvertible types; cannnot cast Preferenceエラーが出る場合== | ||
import android.support.v7.preference.Preference; | import android.support.v7.preference.Preference; | ||
− | Preference screen = (Preference) getPreferenceManager().findPreference( | + | Preference screen = (Preference) getPreferenceManager().findPreference("key1"); |
+ | or | ||
+ | Preference editTextPreference = (Preference) findPreference("key1"); | ||
==参考== | ==参考== |
2018年12月27日 (木) 14:58時点における版
目次
Fragmentについて
Fragmentについて理解してない場合はこちら
android/Fragment [ショートカット]
PreferenceFragmentCompatを追加
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.PrefsFragment; 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, PrefsFragment.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>
inconvertible types; cannnot cast Preferenceエラーが出る場合
import android.support.v7.preference.Preference; Preference screen = (Preference) getPreferenceManager().findPreference("key1");
or
Preference editTextPreference = (Preference) findPreference("key1");
参考
【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