Java 在MainActivity的片段中显示EditText的值

Java 在MainActivity的片段中显示EditText的值,java,android,android-fragments,sharedpreferences,preferences,Java,Android,Android Fragments,Sharedpreferences,Preferences,我需要在TextView中显示EditTextpreference(设置活动)的值,它位于我的MainActivity片段中。我已经尝试过几种解决方案,我的问题主要是将值保存在所有活动都可以访问的变量中。我听说过两种方式:意向附加和共享参考。问题是,在我所看到的所有教程中,TextView都在MainActivity中,而不是它的片段,而且我尝试的代码在那里不起作用。以下是我的xml设置: <PreferenceScreen xmlns:android="http://schemas.an

我需要在TextView中显示EditTextpreference(设置活动)的值,它位于我的MainActivity片段中。我已经尝试过几种解决方案,我的问题主要是将值保存在所有活动都可以访问的变量中。我听说过两种方式:意向附加和共享参考。问题是,在我所看到的所有教程中,TextView都在MainActivity中,而不是它的片段,而且我尝试的代码在那里不起作用。以下是我的xml设置:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <!--CheckBoxPreference
        android:key="example_checkbox"
        android:title="@string/pref_title_social_recommendations"
        android:summary="@string/pref_description_social_recommendations"
        android:defaultValue="true" /-->

    <!-- NOTE: EditTextPreference accepts EditText attributes. -->
    <!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
    <EditTextPreference
        android:key="example_text"
        android:title="@string/pref_title_display_name"
        android:defaultValue="@string/pref_default_display_name"
        android:selectAllOnFocus="true"
        android:inputType="textCapWords"
        android:capitalize="words"
        android:singleLine="true"
        android:maxLines="1"
        android:maxLength="25" />

    <!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
         dismiss it. -->
    <!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
    <ListPreference
        android:key="example_list"
        android:title="@string/pref_title_add_friends_to_messages"
        android:defaultValue="5"
        android:entries="@array/pref_example_list_titles"
        android:entryValues="@array/pref_example_list_values"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null" />

</PreferenceScreen>
所以我的主要目标是创建一个变量,它保存edittextpreference的值,然后在

textView.setText(Variable here);

如何实现这一点?

所有首选项值都存储在默认的共享首选项文件中,并带有相应的键:

     View rootView;
     onCreateView(){
        rootView=inflate(" inflate your view here");
        return rootView;

     }
     onResume(){
           TextView textView=(TextView)rootView.findViewById("your resource id here")
           SharedPreference pref=PreferenceManager.getDefaultSharedPreference(getActivity());
    String text=pref.getString("example_text","")
    textView.setText(text);

     }

代码似乎正常工作,但带有getString的部分抛出错误:无法解析符号示例\文本。可能是我必须以某种方式定义它或创建一个locar变量吗?请现在尝试“example_text”是您作为编辑文本首选项的键指定的键编辑:是的,这是有效的。还有一个扩展:文本仅在加载apk时更改,这意味着如果我转到preferences活动,更改值,然后返回Main活动,它不会立即更改。您能帮我解决这个问题吗?@string/pref\u default\u display\u name这个字符串中有什么内容您在扩展首选项活动或首选项片段吗
     View rootView;
     onCreateView(){
        rootView=inflate(" inflate your view here");
        return rootView;

     }
     onResume(){
           TextView textView=(TextView)rootView.findViewById("your resource id here")
           SharedPreference pref=PreferenceManager.getDefaultSharedPreference(getActivity());
    String text=pref.getString("example_text","")
    textView.setText(text);

     }