简体   繁体   中英

How to get a selected item from a ListPreference

Im working on a little notepad for a card game and I want to be able to choose the amount of players via a settings activity. I managed to create a preference screen with a ListPreference where the amount of players can be selected. How can I get and refer to this selection in my MainActivity.class so I can do something like this:

if(amountPlayers < 3){
        editText3main.setKeyListener(null);
        editText4main.setKeyListener(null);
    }

Thanks in advance.

ListPreference will store a string into the SharedPreferences. To retrieve it, use

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
String defaultValue = "-1"; // assign some meaningful default value
int amountPlayers = Integer.parseInt(sharedPref.getString("NUM_OF_PLAYERS_PREF_KEY", defaultValue));

More on this in android docs .

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