0

私は、xmlレイアウトの私のxmlフォルダにpreferenceactivityと私のpreferences.xmlレイアウトを拡張するEditPreferences.classを持っています。レイアウトet_layout.xmlを持つEditTextPreferenceコントロールを使用しました私の質問は、私のEditPreferences.classでテキストビュー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

EditPreferences.onCreate の単純な findViewById が機能しない理由は、TextView を使用したレイアウトが ListView 内で作成され、アクティビティが起動されたときに実際に発生するまでに遅延があるためです。

これが最善の方法かどうかはわかりませんが、ListView 項目の作成のイベントを処理する 1 つの方法は、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 に答える