我怎样才能在一个嵌套的偏好设置屏幕访问一个TextView

问题描述:

我试图达到并通过其位于子偏好屏幕ListPreference更新的TextView,但没有成功我怎样才能在一个嵌套的偏好设置屏幕访问一个TextView

FILE:“preference.xml”

PreferenceScreen> 

    blah... blah... 

    <PreferenceScreen> 

     <ListPreference 
      android:id="@+id/pref_key_myNotification_time" 
      android:key="@string/pref_key_myNotificationTime" 
      android:title="@string/choose_time_hour_title" 
      android:defaultValue="24 Hours" 
      android:entries="@array/notify_hour" 
      android:entryValues="@array/notify_hourValues" 
      android:layout="@layout/my_notifications_layout"/> 

    </PreferenceScreen> 

</PreferenceScreen> 

FILE: “my_notification_layout.xml”

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 


    <LinearLayout style="@style/myLayoutStyle" 
       android:theme="@style/MyAppTheme"> 

    <TextView style="@style/myPrefStyle" 
       android:text="Pick Notification Hours"/> 

    <TextView android:id="@+id/notifyMe_chooseTime_textView" 
       android:layout_height="wrap_content" 
       android:layout_width="fill_parent" 
       android:text="24 Hours"/> 

    </LinearLayout> 

    <View style="@style/PrefDivider"/> 

</LinearLayout> 
在我的 “preference_frag.java”

现在我试图访问 “notifyMe_chooseTime_textView”,但失败了!

@Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    { 
/* This doesn’t seem to be working!! */ 
if (pref.getKey().equals(“key_notifyMeChooseTime”)) 
     { 
     TextView tvHours = (TextView) getView().findViewById(“notifyMe_chooseTime_textView”); 
     tvHours.setText(pref.getEntry().toString()); 
} 
    } 

如果你们中的任何人都能找到解决方案,我将不胜感激! 感谢

我是新到Android不知道这是否会工作,但尝试这个

@Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    { 
/* This doesn’t seem to be working!! */ 
if (pref.getKey().equals(“key_notifyMeChooseTime”)) 
    { 
    TextView tvHours = (TextView).findViewById(R.id.notifyMe_chooseTime_textView); 
     tvHours.setText(pref.getEntry().toString()); 
} 
} 
+0

是的,事实上,R.id.notifyMe_chooseTime_textView是我如何在我的项目完成。不真正可访问 – beachboy2009