我正在運行一項服務。在此我想讀取啓動它的應用程序的首選項。儘管我可以閱讀其他偏好。我無法讀取列表首選項中的選定項目。請幫忙。代碼或相關鏈接表示讚賞。 在此先感謝。獲取listPreference選擇的項目。 Android
1
A
回答
3
創建文件夾/ RES /值的XML文件arrays.xml/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listDisplayWord">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
</string-array>
<string-array name="listReturnValue">
<item>1 is selected</item>
<item>2 is selected</item>
<item>3 is selected</item>
</string-array>
</resources>
在文件夾/ RES/XML/
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:title="List Preference"
android:summary="Select the option of the list"
android:key="listPref"
android:entries="@array/listDisplayWord"
android:entryValues="@array/listReturnValue" />
</PreferenceScreen>
創建SetPreferences.java創建的preferences.xml
package com.exercise.AndroidListPreference;
import com.exercise.AndroidListPreference.R;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SetPreferences extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
主代碼
package com.exercise.AndroidListPreference;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidListPreference extends Activity {
TextView myListPref;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListPref = (TextView)findViewById(R.id.list_pref);
Button buttonSetPreference = (Button)findViewById(R.id.setpreference);
buttonSetPreference.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(AndroidListPreference.this, SetPreferences.class));
}});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
String myListPreference = myPreference.getString("listPref", "default choice");
myListPref.setText(myListPreference);
}
}
也有修改的AndroidManifest.xml包括SetPreferences.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidListPreference"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidListPreference"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetPreferences" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
+0
在任何佈局文件中,都沒有提到ListPreference的id。但是你已經在findViewById中提到過它。關鍵與id相同。另外,我想在我的服務中讀取這些首選項,但沒有此名稱的findViewById方法。 – harshit
相關問題
- 1. 如何從ListPreference中獲取選定的項目
- 2. 獲取MX:AdvancedDataGrid選擇項目
- 3. 獲取AlertDialog選擇項目
- 4. 獲取MultiSelectList選擇項目
- 5. 如何從ListPreference中選擇選項?
- 6. 滾動到ListPreference中的選定項目
- 7. Android的ListPreference不保存選擇
- 8. 的Android ListPreference填寫項目onPreferenceClick事件
- 9. 每次選擇項目時獲取選定的項目名稱
- 10. 獲取ListPreference選項的關鍵字(不是值) - 可能嗎?
- 11. Custom ListPreference RadioButton不保存所選項目
- 12. Android:將EditTextPreference添加爲ListPreference項目
- 13. 獲取簡單列表項單項選擇題 - 獲取選定的項目
- 14. ExpadableListView獲取選定的項目android
- 15. Android ListView取消選擇項目
- 16. 在多次選擇iconview中獲取上次選擇的項目?
- 17. 如何從加載ListPreference的選定Spinner項中獲取值?
- 18. Eclipse/Android:「選擇項目」缺少選擇?
- 19. 如何獲取選定的文本和值Android ListPreference
- 20. Android上的多選ListPreference
- 21. Android:從ListView中獲取所選項目
- 22. 如何在xamarin android上選擇listview時獲取項目?
- 23. Android:選擇列表視圖項目並獲取對象
- 24. 選擇選項 - 無法獲取選項
- 25. 無法獲取listPreference在Android中工作
- 26. Android Listview項目選擇
- 27. 列表視圖選擇並取消選擇項目Android
- 28. ListPreference沒有選擇。如何?
- 29. 如何在ListPreference中獲取當前默認項目
- 30. 檢查,如果沒有一個ListPreference選擇的選項
起初我以爲我可以得到一個SharedPreferences對象使用PreferenceManager.getDefaultSharedPreferences(本),然後調用get字符串的方法與主要作爲自變量ListPreference。但是,它沒有工作。我嘗試爲此首選項獲取編輯器對象,然後使用它獲取選定的值。但是,它只提供了設置事物的方法。 – harshit