0

我有一个 EditPreferences.class,它在我的 xml 布局中的 xml 文件夹中扩展了preferenceactivity 和我的preferences.xml 布局我使用了具有布局 et_layout.xml 的 EditTextPreference 控件我的问题是如何在我的 EditPreferences.class 中获取我的 textview id

  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  android:title="@string/app_name" >
  <PreferenceCategory
    android:icon="@drawable/batitle"
    android:textColor="#000000"
    android:title="كۆرۈنۈش" >
    <ListPreference
        android:defaultValue="@string/result_fontsize_default"
        android:dialogTitle="تاللاڭ"
        android:entries="@array/result_fontsize_keys"
        android:entryValues="@array/result_fontsize_values"
        android:key="result_fontsize"
        android:summary="ئىزدەش نەتىجىسىنىڭ كۆرۈنۈش چوڭلۇقى"
        android:textColor="#000000"
        android:title="خەت چوڭلۇقى" />

    <EditTextPreference
        android:dialogIcon="@drawable/batitle"
        android:dialogTitle="editText"
        android:key="tel"
        android:layout="@layout/layout" />

    <Preference />
   </PreferenceCategory>

   </PreferenceScreen>

布局

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

 </LinearLayout>
4

3 回答 3

0

尝试这个,

SharedPreferences prefs;
String x = prefs.getString("tel",defaultString value);
textView.setText(x);
于 2014-01-30T06:06:59.073 回答
0

EditPreferences.onCreate 中的简单 findViewById 不起作用的原因是,您的 TextView 布局是在 ListView 内创建的,并且在启动活动时实际发生之前存在延迟。

我不确定这是最好的方法,但处理 ListView 项填充事件的一种方法是处理 ListView 对象的 OnHierarchyChange 事件。如果你把它放在你的 onCreate 中,在从我假设是对 addPreferencesFromResource 的调用加载首选项布局之后,它应该可以工作。

ListView list = (ListView) findViewById(android.R.id.list);
list.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
    @Override
    public void onChildViewAdded(View parent, View child) {
        TextView text = (TextView) child.findViewById(R.id.textView2);
        if (text != null) {
            text.setText("Something else here");
        }
    }

    @Override
    public void onChildViewRemoved(View view, View view2) {
    }
});
于 2014-01-30T10:54:23.380 回答
0

膨胀你的xml

    LayoutInflater inflater = LayoutInflater.from(this);
    View view = inflater.inflate(R.layout.linear_layout, null);

    textView2 = (TextView) view.findViewById(R.id.textView2);

试试这个,希望对你有帮助

于 2014-01-30T05:33:37.220 回答