2016-07-07 7 views
0

私はさまざまな断片を膨張させるために使用される親のレイアウトを持っています。そのようなフラグメントレイアウトの1つでは、フラグメントクラス内のビュー要素にアクセスする必要があります。 以下のコードを使用して、フラグメントレイアウト内のテキストビューの参照を取得します。フラグメントが別のレイアウト内で膨張している場合、フラグメントのビューの参照を取得するにはどうすればよいですか?

CreateTaskFragment.java

@Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     View view = getView(); 
     endDateView = (TextView)view.findViewById(R.id.estimated_end_input); 
     endDateView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Log.i("Date","I'm clicked"); 
      } 
     }); 
    } 

endDateViewヌルを返しています。

fragment_create_task.xml

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/estimated_end_date" 
     android:textSize="15sp" 
     android:layout_marginTop="10dp" 
     android:layout_marginLeft="15dp" 
     android:id="@+id/estimated_end_label"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/task_end_date_input" 
     android:textSize="18sp" 
     android:inputType="date" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="15dp" 
     android:layout_below="@+id/estimated_end_input"/> 

</LinearLayout> 

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/drawer_layout"> 

    <!-- The Main Content Layout --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.FloatingActionButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_margin="25dp" 
      app:elevation="4dp" 
      android:clickable="true" 
      android:src="@drawable/ic_create_black_24dp" 
      android:id="@+id/fab_button"/> 
    </FrameLayout> 

    <!-- The Navigation Drawer--> 
    <ListView 
     ... 
    </ListView> 

</android.support.v4.widget.DrawerLayout> 

にはどうすればCreateTaskFragmentのビュー要素への参照を取得できますか?あなたのフラグメントにおける方法下記

+0

コピーをごonActivityCreatedメソッドを削除し、フラグメント – Drv

答えて

0

コピーおよびフラグメント

からあなたのフラグメント内のメソッド以下
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_create_task, container, false); 

    TextView endDateView = (TextView)rootView .findViewById(R.id.estimated_end_input); 
    endDateView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Log.i("Date","I'm clicked"); 
     } 
    }); 

return rootView; 

} 
+0

からあなたonActivityCreatedメソッドを削除それは私がレイアウトでIDを定義されていなかった、実際に働いていました私はfindViewById()メソッドに渡していました。 – Paras

関連する問題