如何以不同的布局更新TextView

时间:2014-02-09 05:00:37

标签: android android-layout android-fragments android-view

I'm just trying to update TextView (@id/pref_gender_textView) in the "gender.xml" where gender.xml is defined in preference.xml.  I'd like to update the TextView from MyPreferenceFragment.java

如何从MyPreferenceFragment.java更新TextView genderTextView?
非常感谢你!

  • gender.xml:

    <LinearLayout style="@style/PrefHorizontalLayout2"
                  android:theme="@style/AppTheme">
    
        <TextView style="@style/PrefTextTitle2"
                  android:text="@string/pref_gender"/>
    
        <TextView android:layout_height="wrap_content"
                  android:layout_width="fill_parent"
                  android:id="@+id/pref_gender_textView"
    </LinearLayout>
    

- preference.xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
            android:key="lp_gender"
            android:entries="@array/array_lp_gender"
            android:entryValues="@array/array_lp_gender_values"
            android:title="@string/str_lp_android_choice_gender"
            android:layout="@layout/gender"/>

</PreferenceScreen>

现在,我想从“MyPreferenceFragment.java”中的某处访问“genderTextView”

import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyPreferenceFragment extends PreferenceFragment{   

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        /** Defining PreferenceChangeListener */
        OnPreferenceChangeListener onPreferenceChangeListener = new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {             
                OnPreferenceChangeListener listener = ( OnPreferenceChangeListener) getActivity();
                listener.onPreferenceChange(preference, newValue);
                return true;
            }
        };  

    }

}

1 个答案:

答案 0 :(得分:1)

Android框架负责将设备中应用程序的/ data目录中的XML文件中的首选项持久化。 API提供了几种方法来处理存储的首选项。

您真的不想在不同的布局中更新TextView。这样做会在您的首选项视图和其他视图之间引入不必要的紧密耦合。你真正想要的只是在设置发生变化时更新TextView中的文本。这可以通过实现SharedPreferences.OnSharedPreferenceChangeListener来实现。为此,请注册您的活动为监听器:

public class YourActivity extends Activity implements OnSharedPreferenceChangeListener {

   private TextView mMyText;

   @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        mMyText = (TextView) findViewById(android.R.id.my_text);

        // listen to preference changes
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPref.registerOnSharedPreferenceChangeListener(this);

        ...
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {

        if(key.equals(getString(R.string.pref_key_myText))){

            mMyText.setText(sharedPreferences.getString(key, getString(R.string.pref_myText_defaultValue)));
        }
    }
}