2014-02-09 97 views
0
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?
非常感謝您提前!如何更新不同佈局的TextView

  • 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> 

現在,我想訪問「genderTextView」從某處「MyPreferenceFragment.java」

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; 
      } 
     }; 

    } 

} 
+0

性別TextView在哪裏?另一部分活動還是什麼? –

回答

1

Android框架需要對應用程序/數據目錄中的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))); 
     } 
    } 
}