Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
186
CHAPTER 8: APIs LIST -> { findPreference(key).summary = (findPreference(key) as ListPreference). entry } RINGTONE -> { val uriStr = getString(key, "") ?: "" findPreference(key).summary = getRingtoneName(Uri.parse(uriStr)) } } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences) val sharedPref = PreferenceManager. getDefaultSharedPreferences(activity) sharedPref.registerOnSharedPreferenceChangeListener( this) with(sharedPref) { findPreference(DELETE_LIMIT).summary = getString(DELETE_LIMIT, "10") findPreference(LIST).summary = (findPreference(LIST) as ListPreference).let { val ind = Math.max(0, it.findIndexOfValue( it.value)) resources.getStringArray(listentries)[ind] } findPreference(RINGTONE).summary = getRingtoneName( Uri.parse(getString(RINGTONE, "") ?: "")) } } fun getRingtoneName(uri:Uri):String { return activity.contentResolver. query(uri, null, null, null, null)?.let { it.moveToFirst() val res = it.getString( it.getColumnIndex( MediaStore.MediaColumns.TITLE)) it.close() res } ?: "" } } 187 CHAPTER 8: APIs This defines a fragment that has the following parameters: On creation, it sets the preferences resource, as described in a moment. On creation, it accesses the preferences API using the PreferenceManager. We use it to set a preferences change listener, so we can update the UI when preferences change. In addition, we fetch a couple of preferences to prepare the preferences UI and set “summary” lines that get shown inside preference items. The listener onSharedPreferenceChanged() is used to update the summary lines for applicable preferences. The actual preferences layout gets defined inside res/xml/, in this case, as a resources file preferences.xml with these contents: "http://schemas.android.com/apk/res/android"> android:summary="Auto delete" android:title="Auto delete" android:defaultValue="false" /> android:dependency="pref_key_auto_delete" android:summary="Delete Limit" android:title="Delete Limit" android:defaultValue="10" /> android:title="A List" android:entries="@array/listentries" android:entryValues="@array/listvalues" android:defaultValue="1" /> android:summary="A Multiselect List" android:title="A Multiselect List" android:entries="@array/listentries" android:entryValues="@array/listvalues" android:defaultValue="@array/multiselectdefaults"/> android:summary="A Switch" android:title="A Switch" android:defaultValue="false" /> android:summary="A Ringtone" android:title="A ringtone" /> |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling