使用首选项更改文本

时间:2014-04-11 16:41:05

标签: android sharedpreferences preferences

我试图在FragmentPagerAdapter内创建一个包含某些活动的应用程序,并且我想根据所选的首选项更改文本。

preferencias.xml

<PreferenceScreen 
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferencias_principal" >
    <ListPreference
        android:key="formato"
        android:title="Formato"
        android:summary="Escoja el formato de entrada de texto"
        android:entries="@array/formato"
        android:entryValues="@array/formatoValores"
        android:defaultValue="1" />
</PreferenceScreen>

arrays.xml

<resources>
<string-array name="formato">
    <item>Format 1</item>
    <item>Format 2</item>
    <item>Format 3</item>
</string-array>

<string-array name="formatoValores">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>
</resources>

Myclass.java

public class Myclass extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.acruz, container, false);
    return rootView;
    }
}

最后, acruz.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
style="@style/Relative" >

     <TextView
        android:id="@+id/tv1"
        style="@style/TextoSmallCursiva"
        android:text="This is a text" />

 </RelativeLayout>

我想要做的是,例如,当我选择 arrays.xml 中的Format 1项时,它会将tv1文本视图更改为另一个文本。< / p>

非常感谢!

1 个答案:

答案 0 :(得分:1)

我相信你必须以编程方式设置文本:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.acruz, container, false);

    TextView tv1 = (TextView) rootView.findViewById(R.id.tv1);

    String selectedOption = // get your preference as string

    tv1.setText(selectedOption);

    return rootView;
    }
}