java - 获取 listPreference 选定的项目。安卓

标签 java android android-preferences android-sharedpreferences

我正在运行一项服务。我想读取启动它的应用程序的首选项。虽然我能够阅读其他偏好。我无法读取列表首选项中的选定项目。请帮忙。代码或相关链接值得赞赏。 提前致谢。

最佳答案

在文件夹/res/values/中创建 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/中创建preferences.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

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>

关于java - 获取 listPreference 选定的项目。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120383/

相关文章:

java - 具有多个首选项文件的 PreferenceActivity

java - 在 EditTextPreference 上设置输入类型

java - 在 java 中使用 JSON 到 bitbucket 服务器的 HTTP POST 返回 400 作为响应代码

java - GWT 的各种异步设施

PHP - 来自 mysql 的 json_decode

android - 为什么使用AndroidX时嵌套的PreferenceScreen打不开?

java - 如何通过单击按钮将单独的字符串添加到数组中,并通过单击不同的按钮从数组中删除字符串?

java - 修复强制转换 List<ArrayObject> 中的警告

android - 使用 Retrofit observable 处理网络错误

android - 居中复选框 View