https://developer.android.com/guide/topics/ui/settings
1) Crea uno simple Configuraciones pantalla usando Biblioteca de preferencias de AndroidX
2) La pantalla de configuración contiene una jerarquía de ajustes preestablecidos. Puedes definirlo jerarquía como XML fuente, o puede construir jerarquía en código.
3) La marca de la raíz debe ser una y el recurso XML debe estar ubicado en un archivo directorio res / xml /.
4) Crea la jerarquía como XML
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"><PreferenceCategory app:title="@string/messages_header">
<EditTextPreference
app:key="signature"
app:title="@string/signature_title"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/sync_header">
<SwitchPreferenceCompat
app:key="sync"
app:title="@string/sync_title" />
<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/attachment_title" />
</PreferenceCategory>
</PreferenceScreen>
Para descubrir más componentes
https://developer.android.com/guide/topics/ui/settings/components-and-attributes
5) Inflar la jerarquía
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
6) Divida su jerarquía en múltiples pantallas
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto"><Preference
app:title="@string/basic_preferences"
app:summary="Sample preferences using basic attributes"
app:fragment="com.example.SyncFragment"/>
//---
Actividad
class MyActivity : AppCompatActivity(),
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {...
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
// Instantiate the new Fragment
val args = pref.extras
val fragment = supportFragmentManager.fragmentFactory.instantiate(
classLoader,
pref.fragment)
fragment.arguments = args
fragment.setTargetFragment(caller, 0)
// Replace the existing Fragment with the new Fragment
supportFragmentManager.beginTransaction()
.replace(R.id.settings_container, fragment)
.addToBackStack(null)
.commit()
return true
}
}
7) Búsqueda de presets
val signaturePreference: EditTextPreference? = findPreference("key")signaturePreference?.setOnPreferenceChangeListener { preference, newValue ->
Toast.makeText(context, newValue.toString(), Toast.LENGTH_LONG).show()
true
}