Android活动有一个片段,我想从这个片段调用另一个片段,我怎样才能实现这个

时间:2016-02-15 08:54:55

标签: android android-fragments

我有activity RegistrationActivity

public class RegistrationActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);



    }

在这个课程中有一个片段 RegistrationActivityFragment

    public class RegistrationActivityFragment extends Fragment {

Button b1;
EditText et1;

public RegistrationActivityFragment() {
}


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_registration_details, container, false);
    b1 = (Button) v.findViewById(R.id.nameNext);
    et1=(EditText)v.findViewById(R.id.fragment_register_details_name);
    showInputMethod();
    et1.requestFocus();
    et1.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable arg0) {

            enableSubmitIfReady();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    b1.setEnabled(false);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EducationalDetailsActivityFragment nextFrag= new EducationalDetailsActivityFragment();
            getActivity().getFragmentManager().beginTransaction()
                    .replace(R.id.fragment, nextFrag)
                    .addToBackStack(null)
                    .commit();

            InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.hideSoftInputFromWindow(b1.getWindowToken(), 0);
        }
    });

    return v;


}


public void showInputMethod() {

    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}


public void enableSubmitIfReady() {

    boolean isReady =et1.getText().toString().length()>3;
    if(isReady){
    b1.setEnabled(true);
    b1.setBackgroundColor(getResources().getColor(R.color.deeppurple500));


    }
    else{
        b1.setEnabled(false);
    }

}

这是片段布局文件

<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"
    android:id="@+id/fragregistrationname"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:fontFamily="sans-serif-thin"
        android:paddingLeft="16dp"
        android:text="We're setting up your profile.     Lets begin with the introduction"
        android:textColor="@color/black"
        android:textSize="25dp"
        android:textStyle="italic" />


    <TextView
        android:id="@+id/reg_details_name_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:paddingLeft="16dp"
        android:text="Name"
        android:textColor="@color/regdetailsnamecolor"
        android:textSize="21dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:background="@color/greyback"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:padding="16dp"
            android:textColorHint="#bcbec0"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            app:hintTextAppearance="@style/TextAppearance.AppCompat">

            <EditText
                android:id="@+id/fragment_register_details_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-thin"
                android:gravity="left"
                android:hint="FULL NAME"
                android:inputType="textPersonName"
                android:textColorHint="#bcbec0" />

        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/nameNext"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@color/buttonDisabledback"
            android:enabled="false"
            android:text="Next"
            android:textColor="@color/white" />

        <TextView
            android:layout_gravity="end"
            android:text="1/4"
            android:layout_width="40dp"
            android:layout_height="40dp" />



    </LinearLayout>

</LinearLayout>

我想点击上面片段中的这个片段

public class EducationalDetailsActivityFragment extends Fragment {

    public EducationalDetailsActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v= inflater.inflate(R.layout.fragment_educational_details2, container, false);
        Button b1= (Button)v.findViewById(R.id.eduNext);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaleFemaleRegistrationActivityFragment nextFrag= new MaleFemaleRegistrationActivityFragment();
                getActivity().getFragmentManager().beginTransaction()
                        .replace(R.id.fragEducational, nextFrag)
                        .addToBackStack(null)
                        .commit();

            }
        });

        return v;

    }

这是第二个片段的布局

<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"
    android:id="@+id/fragEducational" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:fontFamily="sans-serif-thin"
        android:paddingLeft="16dp"
        android:text="Hi Kaustubh , tell us a little about your.."
        android:textColor="@color/black"
        android:textSize="25dp"
        android:textStyle="italic" />


    <TextView
        android:id="@+id/reg_details_name_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:paddingLeft="16dp"
        android:text="Educational Qualifications"
        android:textColor="@color/regdetailsnamecolor"
        android:textSize="21dp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:background="@color/greyback"
        android:orientation="vertical">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:paddingLeft="16dp"
                android:textSize="20dp"
                android:layout_margin="16dp"
                android:text="Bachelors"
                android:layout_weight="0.3"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />

            <Switch
                android:id="@+id/on_off_switch"
                android:layout_margin="16dp"
                android:layout_width="4dp"
                android:layout_gravity="center"
                android:gravity="center"

                android:layout_weight="0.1"
                android:textColor="@color/black"
                android:layout_height="wrap_content"
                android:textOff="MAsters"
                android:textOn="Bachelors"/>

            <TextView
                android:paddingRight="16dp"
                android:textSize="20dp"
                android:layout_margin="16dp"
                android:text="Masters"
                android:layout_weight="0.3"
                android:layout_width="0dp"
                android:layout_height="wrap_content" />


        </LinearLayout>


        <Button
            android:id="@+id/eduNext"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@color/deeppurple500"
            android:text="Next"
            android:textColor="@color/white" />
        <TextView
            android:layout_gravity="end"
            android:text="2/4"
            android:layout_width="40dp"
            android:layout_height="40dp" />


    </LinearLayout>

</LinearLayout>

这是我的活动布局activity_registration

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.purplesq.purplesq.activities.RegistrationActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout="@layout/fragment_registration" >

</fragment>

5 个答案:

答案 0 :(得分:1)

很容易就可以做到这一点。但是片段将替换你的RegistrationActivity布局。你忘了添加这个活动布局(activity_registration)。你可以添加这个布局。所以我可以实现其他部分。

答案 1 :(得分:1)

您必须在RegistrationActivity中使用FrameLayout,因此您可以将片段充气到其中。像这样:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"
>

之后点击任何小部件调用:

 android.support.v4.app.FragmentManager fragmentManager=getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.content_frame, new EducationalDetailsActivityFragment());
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

答案 2 :(得分:1)

我猜你正在寻找的是:

 FragmentTransaction transaction =
 getSupportFragmentManager().beginTransaction();
 transaction.replace(R.id.fragment_container, fragment);
 transaction.commit();

其中R.id.fragment_container是您的活动ID的FrameLayout,其中您已加载了第一个片段,并且片段为&#39;是你的新片段。 您必须在活动中调用此方法,因此您必须创建一个正在执行此操作并从您的片段中调用它的公共方法:

((RegistrationActivity) getActivity()).yourPublicMethod()

答案 3 :(得分:1)

我可以提议你。请你这样做。 你可以添加到otto library.It喜欢iterfaces.it这么容易使用。根据我的错误调用活动方法来自fragment.We有两种方式 首先 你可以使用fragmentIteractionListener。其次你可以使用otto库。otto

奥托是如此的帮助。如果你需要更多的帮助,我可以。

答案 4 :(得分:1)

我认为你在EducationalDetailsActivityFragment onClick()中给出了错误的id(R.id.fragEducation)。你应该用id(R.id.fragment)替换它,如下面的代码所示。

MaleFemaleRegistrationActivityFragment nextFrag= new MaleFemaleRegistrationActivityFragment();
                getActivity().getFragmentManager().beginTransaction()
                        .replace(R.id.fragment, nextFrag)
                        .addToBackStack(null)
                        .commit();