简体   繁体   中英

How to open ListPreference immediately when activity is created?

I have an application with two screens, MainActivity - screen with buttons, and SecondActivity - settings screen. In this SecondActivity I have one ListPreference.

How to open this ListPreference when SecondActivity is opening?

You first need to calculate the position of ListPreference in your PreferenceScreen . Then try this code:

PreferenceScreen main = (PreferenceScreen)findPreference(getString(R.string.pref_screen_root));
    main.onItemClick(null, null, pos, 0);

Here pos is the position of ListPreference in PreferenceScreen . R.string.pref_screen_root is the key of your PreferenceScreen .

Here is the simplest xml file of:

  <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="@string/pref_screen_root" >
 <ListPreference
            android:defaultValue="0"
            android:entries="@array/list_entries"
            android:entryValues="@array/list_values"
            android:icon="@drawable/ic_earth"
            android:key="@string/pref_country"
            android:title="Select a country" />
</PreferenceScreen>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM