简体   繁体   English

如何从ListPreference获取选定的项目

[英]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. 我设法创建了一个带有ListPreference的首选项屏幕,可以在其中选择玩家数量。 How can I get and refer to this selection in my MainActivity.class so I can do something like this: 如何获得并在MainActivity.class中引用此选择,以便可以执行以下操作:

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

Thanks in advance. 提前致谢。

ListPreference will store a string into the SharedPreferences. ListPreference将一个字符串存储到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 . 在android docs中对此有更多的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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