[英]How to get a selected item from a ListPreference
我正在為紙牌游戲制作一個小記事本,我希望能夠通過設置活動來選擇玩家數量。 我設法創建了一個帶有ListPreference的首選項屏幕,可以在其中選擇玩家數量。 如何獲得並在MainActivity.class中引用此選擇,以便可以執行以下操作:
if(amountPlayers < 3){
editText3main.setKeyListener(null);
editText4main.setKeyListener(null);
}
提前致謝。
ListPreference將一個字符串存儲到SharedPreferences中。 要檢索它,請使用
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));
在android docs中對此有更多的了解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.