Android 如何从父活动中的片段获取数据?

Android 如何从父活动中的片段获取数据?,android,android-fragments,Android,Android Fragments,这是我的父活动,包含选项卡布局和视图寻呼机以及底部的按钮: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android

这是我的父活动,包含选项卡布局和视图寻呼机以及底部的按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".module.addcontact.AddContactActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.PopupOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabTextColor="@color/colorDarkBlue"
            app:tabSelectedTextColor="@color/colorDarkBlue"
            android:background="@color/white"/>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:text="SAVE"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:layout_margin="10dp"
        android:background="@color/colorDarkBlue"/>

</LinearLayout>

片段1:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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:gravity="center_horizontal"
    android:fillViewport="true"
    android:padding="@dimen/add_contacts_padding"
    tools:context=".module.addcompany.AddCompanyFragment">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="Name"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone"
        android:hint="Phone"
        android:inputType="phone"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:hint="Address"
        android:textSize="24sp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/edit_text_border"/>

    </TableLayout>

</ScrollView>

片段2与片段1相似(具有不同的编辑文本,例如名称和电子邮件)

当按下父活动中的“保存”按钮时,我希望从父活动中的这两个片段中获取所有数据,如果某个字段丢失,则我希望将错误设置为该字段

如何将数据从片段获取到父活动? 或
是否有某种方法可以获取父活动中片段的所有编辑文本(字段)?

要允许片段与其活动通信,可以在片段类中定义接口并在活动中实现它

以下是片段到活动通信的示例:

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener callback;

    public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
        this.callback = callback;
    }

    // This interface can be implemented by the Activity, parent Fragment,
    // or a separate test implementation.
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    // ...
}
main活动

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{


    @Override
    public void onAttachFragment(Fragment fragment) {
        if (fragment instanceof HeadlinesFragment) {
            HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
            headlinesFragment.setOnHeadlineSelectedListener(this);
        }
    }
}
例如,当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    callback.onArticleSelected(position);
}

希望它对您有用。

要允许片段与其活动通信,您可以在Fragment类中定义一个接口,并在活动中实现它

以下是片段到活动通信的示例:

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener callback;

    public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
        this.callback = callback;
    }

    // This interface can be implemented by the Activity, parent Fragment,
    // or a separate test implementation.
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    // ...
}
main活动

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{


    @Override
    public void onAttachFragment(Fragment fragment) {
        if (fragment instanceof HeadlinesFragment) {
            HeadlinesFragment headlinesFragment = (HeadlinesFragment) fragment;
            headlinesFragment.setOnHeadlineSelectedListener(this);
        }
    }
}
例如,当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Send the event to the host activity
    callback.onArticleSelected(position);
}

希望它对您有用。

根据您的要求,您必须创建父活动,这意味着您的片段是使用viewpager加载的,因此您拥有每个片段的实例

现在,当您单击活动中的按钮时,要检查单个片段的有效性并访问数据,您可以这样做

首先检查哪个片段位于哪个位置,哪个位置将是onPageSelected查看寻呼机方法中的位置

现在假设您有Fragment1,并且希望访问数据并对其进行验证。使用

Fragment1 f1= (Fragment1) viewPager
                .getAdapter()
                .instantiateItem(viewPager, position);
现在您需要在
Fragment1
中创建
validate()

f1.validate();

在调用f1.validate()之前;检查
if(f1!=null)&&f1.isVisible()
,然后从中访问数据,并根据您的要求对
Fragment2

执行相同操作。您必须创建父活动,这意味着您的片段是使用viewpager加载的,因此您拥有每个片段的实例

现在,当您单击活动中的按钮时,要检查单个片段的有效性并访问数据,您可以这样做

首先检查哪个片段位于哪个位置,哪个位置将是onPageSelected查看寻呼机方法中的位置

现在假设您有Fragment1,并且希望访问数据并对其进行验证。使用

Fragment1 f1= (Fragment1) viewPager
                .getAdapter()
                .instantiateItem(viewPager, position);
现在您需要在
Fragment1
中创建
validate()

f1.validate();


在调用f1.validate()之前;检查
if(f1!=null)&&f1.isVisible()
然后从中访问数据,并对
片段2
执行相同的操作
活动的布局文件中,您在哪里包括
片段的布局
推出。现在,您必须在片段中创建用于访问数据的方法,并在片段中创建验证方法,并使用当前选定片段的实例在父活动中调用该方法,因为您已经有了片段的实例。您不需要再次创建片段实例。先生@Piyush我是android的新手。你可以写代码行来访问片段站姿吗?在
活动
的布局文件中,你在哪里包括
片段
的布局?可能与Sir@SteliosPapamichail重复?我正在用java代码将我的片段与viewpager一起附加活动启动时,你已经有了所有片段的实例。现在,您必须在片段中创建用于访问数据的方法,并在片段中创建验证方法,并使用当前选定片段的实例在父活动中调用该方法,因为您已经有了片段的实例。您不需要再次创建片段实例。先生@Piyush我是android的新手。你能写代码行来访问片段站姿吗?这是单边bro,活动将收到通知,因为活动正在侦听回调。但根据问题,当用户点击活动按钮时,他想通知fragment。@vikaskumar他在问题“如何从父活动的片段中获取数据?”和问题标题“如何从父活动的片段中获取数据?”中说,但根据这一点,必须有人从片段中推送数据,但按钮处于活动状态。@vikaskumar我将在一段时间内更新活动到片段通信的答案。上面的答案解决了我的问题,但非常感谢您尝试帮助我。这是片面的兄弟,活动将收到通知,因为活动正在侦听回调。但根据问题,当用户点击活动按钮时,他想通知fragment。@vikaskumar他在问题“如何从父活动的片段中获取数据?”和问题标题“如何从父活动的片段中获取数据?”中说,但根据这一点,必须有人从片段中推送数据,但按钮处于活动状态。@vikaskumar我将在一段时间内更新活动到片段通信的答案。上面的答案解决了我的问题,但非常感谢您尝试帮助我。