如何在嵌套首选项屏幕中访问TextView

时间:2014-02-10 17:29:04

标签: android-layout android-fragments textview android-preferences android

我正在尝试通过ListPreference来访问和更新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());
}
  }

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

1 个答案:

答案 0 :(得分:0)

我是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()); 
}
 }