当前位置: 首页 > 知识库问答 >
问题:

Android ListPreference输入文本,而不是来自activity或未扩展PreferenceFragment的片段的输入值

佘京
2023-03-14

在我的应用程序中,我有一个SettingsFragment,该扩展了PreferenceFragmentCompat

在此设置中,我有一个ListPreference通过一个接收JSON值的HTTP调用动态生成。

现在的问题是,从MainActivity中,当我将值存储到SharedPreference中时,我得到了Android.Entries,但我没有找到获取EntryValues的方法。

我看到可以从SettingsFragment中使用GetEntry()方法获取条目:

ListPreference listPreference = (ListPreference)
findPreference("entry_preference");
String entry = listPreference.getEntry();

但这只在SettingsFragment中起作用....如何从MainActivity获取所选条目而不是entryValues?

sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//This will return the value, but i need the entry text
String entry_settings = sharedPref.getString("entry_preference", "-1");

该代码将返回-1,但我不需要输入:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- dynamically generated by reading values from server -->
    <string-array name="entry_names_array">
        <item>No Entry</item>
    </string-array>

    <!-- dynamically generated by reading values from server -->
    <string-array name="entry_values_array">
        <item>-1</item>
    </string-array>
</resources>

如何将该值放入MainActivity中,或者更广泛地说,如何将该值放入不扩展PreferenceFragmentCompatPrefereceActivity的activity/片段中

编辑

我看到MainActivity中sharedPref对象有两个键:

SharedPreference sharedPref = PreferenceManager.getDefaultSharedPreferences(this);

它包含键entry_preference,它是ListPreference的键:

   <ListPreference
        android:key="entry_preference"
        android:entries="@array/entry_names_array"
        android:entryValues="@array/entry_values_array"
        android:summary="%s"
        android:title="@string/listpref_entry_settings_title" />

和另一个键“entry”,但该键返回相同的值“entry_preference”。

我写错了什么,还是正常的是键“entry”返回相同的entry_preference值?

编辑2-可能的答案

现在,我将该条目保存在SharedPreference中,这样我就可以通过阅读它来从MainActivity获取它。我不知道这是不是一个好的方法,但这里的代码。

1)当用户选择其他选项并将其存储到SharedPreferenceChangelistener时,使用OnSharedPreference检查

public class SettingsFragment extends PreferenceFragmentCompat  {
    private static final String TAG = "SettingsFragment";
    private SharedPreferences sharedPref;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        setEntracesUrl();
        requestForEntrancesToServer();

        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                Log.i(TAG, "Settings key changed: " + key);
                ListPreference listPreferenceEntrances = (ListPreference) findPreference("entry_preference");
                String currentEntry = (String) listPreferenceEntrances.getEntry();
                listPreferenceEntrances.setSummary(currentEntry);
                //Write the currentEntry into SharedPreferences
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
                editor.commit();
            }
        };
        sharedPref.registerOnSharedPreferenceChangeListener(prefListener);
    }
}

2)我从http请求接收创建ListPreference的值,因此我需要注意条目、entriesValues和summary

 // Code inside method evaluateJSONObject() that evaluate JSON received 
 //from http request to server that return the values for ListPreference
 String currentEntry = (String) listPreferenceEntrances.getEntry();
 listPreferenceEntrances.setEntries(entries);
 listPreferenceEntrances.setEntryValues(entryValues);
 listPreferenceEntrances.setSummary(currentEntry);
 //Write the currentEntry into SharedPreferences
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
 editor.commit();

3)最后,在MainActivity的onCreate中,我读取sharedPreferences并使用条目文本设置Textview:

   //SharedPreference instance
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    setBaseUrl();

    // This show the string entry VALUE of selected ListPreference
    String entry_settings_id = sharedPref
            .getString("entry_preference", "-1");
    // This show the string entry of selected ListPreference
    String entry_settings_name = sharedPref
            .getString(
                    getString(R.string.current_listpreference_entry_key),
                    getString(R.string.current_listpreference_entry_default_value));

让我知道如果有更好的方法来做我需要的事情!谢谢

共有1个答案

顾磊
2023-03-14

在没有更好的方法之前,我会把我的答案贴出来,也许其他人可以这样做。

1)当用户选择另一个选项并将其存储到SharedPreference时,使用OnSharedPreferenceChangeListener检查

public class SettingsFragment extends PreferenceFragmentCompat  {
    private static final String TAG = "SettingsFragment";
    private SharedPreferences sharedPref;
    private SharedPreferences.OnSharedPreferenceChangeListener prefListener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
        setEntracesUrl();
        requestForEntrancesToServer();

        prefListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                Log.i(TAG, "Settings key changed: " + key);
               //Use a switch or else case that avoid loop call of OnSharedPreferenceChangeListner()
                switch(key) {
                    case "entry_preference":
                        ListPreference listPreferenceEntrances = (ListPreference) findPreference("entry_preference");
                        String currentEntry = (String) listPreferenceEntrances.getEntry();
                        listPreferenceEntrances.setSummary(currentEntry);
                        //Write the currentEntry into SharedPreferences
                        SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
                        editor.commit();
                    break;
               default:
                    break;
              }
            }
        };
        sharedPref.registerOnSharedPreferenceChangeListener(prefListener);
    }
}

2)我从http请求接收创建ListPreference的值,因此我需要注意条目、entriesValues和summary

 // Code inside method evaluateJSONObject() that evaluate JSON received 
 //from http request to server that return the values for ListPreference
 String currentEntry = (String) listPreferenceEntrances.getEntry();
 listPreferenceEntrances.setEntries(entries);
 listPreferenceEntrances.setEntryValues(entryValues);
 listPreferenceEntrances.setSummary(currentEntry);
 //Write the currentEntry into SharedPreferences
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.putString(getString(R.string.current_listpreference_entry_key), currentEntry);
 editor.commit();

3)最后,在MainActivity的onCreate中,我读取sharedPreferences并使用条目文本设置Textview:

   //SharedPreference instance
    sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    setBaseUrl();

    // This show the string entry VALUE of selected ListPreference
    String entry_settings_id = sharedPref
            .getString("entry_preference", "-1");
    // This show the string entry of selected ListPreference
    String entry_settings_name = sharedPref
            .getString(
                    getString(R.string.current_listpreference_entry_key),
                    getString(R.string.current_listpreference_entry_default_value));
 类似资料:
  • 我有三个输入,我想添加,当我把我的第一个数字"5"console.log显示"0"下一个输入字段我输入"2"这将console.log第一个输入字段的数字"5"第三个我输入我在“7”console.log看到的“5”。 为什么会发生这种情况,我该如何解决它。 JavaScript.

  • 问题内容: 类似于下面的JSFiddle(我将其添加为书签,并且不知道原始问题来自何处): 有没有一种方法可以将文本字段的宽度固定为例如200px,如果用户添加的文本超过200px可以容纳的文本的 高度 ,文本字段的 高度 会增加吗?如果用户需要更多空间来键入内容,我希望添加更多行…因此我需要 height 而不是width来动态调整大小。 谢谢! 问题答案: 正如其他人解释的那样,字段不能包含多

  • 问题内容: 我想知道是否有可能扩展Angular的输入指令?我想将一些侦听器附加到页面上的所有输入字段。我认为您可以使用来装饰现有模块,但我不知道如何使用指令(更确切地说是输入指令)来完成此操作。 那么,谁能将我推向正确的方向呢?一些例子? 编辑 到目前为止,这是我的指令: 在我看来,可以将其添加到输入字段中,如下所示: 缺点是,对于每个输入字段,我都必须在代码中手动附加此侦听器。因此,更改现有的

  • 问题内容: 我正在尝试我的React.js的第一部分,并在很早的时候就陷入了困境…我有下面的代码,该代码将搜索表单呈现为。但是,在搜索框中输入内容无济于事。 大概在通过道具并上下移动时会丢失一些东西,这似乎是一个常见问题。但是我很沮丧-我看不到缺少的东西。 (最终,我会使用其他类型的,但是我只是想让这一类正常工作。) 问题答案: 您尚未将道具放在机箱中。它必须在JSX中。 在文档中充分考虑了将 p

  • 问题内容: 我在Goji框架上运行了一些东西: 我希望有人能帮助我做的是弄清楚如何提交HTML表单以将数据发送到Golang代码。 因此,如果存在一个带有name属性的输入字段,并且该属性的值是name,并且用户在其中输入名称并提交,那么在提交的表单页面上,Golang代码将打印问候,名称。 这是我能想到的: 这是我的hello.html文件: 在身体里: 如何连接到使Golang代码获取的是在表

  • 可以输出文本。 用法 Your browser does not support the video tag. 案例:标签标签 功能:显示输入的字母。