Android 区分同一片段的两个实例
我有一个包含片段的活动布局文件:Android 区分同一片段的两个实例,android,android-fragments,Android,Android Fragments,我有一个包含片段的活动布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/RelLayout" android:layout_width="match_parent" android:layout_height="match_parent" to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sguancicosimo.com.planmytrip.NewTrip1"
android:orientation="vertical">
<EditText
android:id="@+id/Where"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/Where"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="85dp"
android:drawableLeft="@drawable/ic_location_city_black_24dp"/>
<ImageView
android:id="@+id/imageButtonDone"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/Where"
android:src="@drawable/ic_done_black_24dp"
android:layout_marginTop="96dp"
android:layout_marginLeft="25dp"
/>
<TextView
android:id="@+id/departureDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/departureDate"
android:layout_marginTop="35dp"
android:layout_marginLeft="15dp"
android:layout_below="@+id/Where"
android:layout_alignParentStart="true"
android:textSize="20sp"
android:textStyle="bold"
android:clickable="true"
android:onClick="showCalFrag"/>
<TextView
android:id="@+id/returnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/returnDate"
android:layout_marginTop="35dp"
android:layout_marginLeft="15dp"
android:layout_below="@+id/departureDate"
android:layout_alignParentStart="true"
android:textSize="20sp"
android:textStyle="bold"
android:clickable="true"
android:onClick="showCalFrag"/>
</RelativeLayout>
片段是在运行时单击两个TextView(showCalFrag方法)时创建的。片段包含在CalendarView中,我希望保留用户选择的日期,并在活动中发送它们,但我不知道如何区分这两个CalendarView,因为它们是同一片段的两个实例,而且我在让用户更改已选择的日期时遇到问题。您可以创建一个
枚举
,如:
public enum FragmentType{
FROM_DATE,TO_DATE
}
并在片段中创建具有此枚举类型的新变量:
private FragmentType fragmentType;
public void setFragmentType(FragmentType type){ this.fragmentType = type;}
调用片段时:
YourFragment fragment = new YourFragment();
fragment.setFragmentType(FragmentType.FROM_DATE); // for the first textView
fragment.setFragmentType(FragmentType.TO_DATE); // for the second textView
然后您可以区分这两个实例您可以创建一个
枚举
如下:
public enum FragmentType{
FROM_DATE,TO_DATE
}
并在片段中创建具有此枚举类型的新变量:
private FragmentType fragmentType;
public void setFragmentType(FragmentType type){ this.fragmentType = type;}
调用片段时:
YourFragment fragment = new YourFragment();
fragment.setFragmentType(FragmentType.FROM_DATE); // for the first textView
fragment.setFragmentType(FragmentType.TO_DATE); // for the second textView
然后您可以区分这两个实例我如何从活动类检索FragmentType的可能副本的可能副本?为FragmentType创建一个getter方法,如:
public FragmentType getFragmentType(){return FragmentType;}
从片段实例调用它以获取类型如何从活动类检索FragmentType?为FragmentType创建一个getter方法,如:public FragmentType getFragmentType(){return FragmentType;}
从片段实例调用它以获取类型