【发布时间】:2019-09-20 19:53:10
【问题描述】:
这是我的父 Activity,包含选项卡布局和视图分页器以及底部的按钮:
<?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(具有不同的编辑文本,例如名称和电子邮件)
当我的父活动中的保存按钮被按下时,我想从父活动中的这两个片段中获取所有数据,如果缺少某些字段,那么我想为该字段设置错误。
如何从片段中获取数据到父活动? 或者 有什么方法可以获取父活动中片段的所有编辑文本(字段)?
【问题讨论】:
-
你在
Activity的布局文件中包含Fragment的布局在哪里? -
@SteliosPapamichail 先生,我正在用 java 代码中的 viewpager 附加我的片段
-
活动启动时,您已经拥有所有片段的实例。现在,您必须在片段中创建用于访问数据的方法,并在片段中创建验证方法,并使用当前选定片段的实例在父活动中调用该方法,因为您已经拥有片段实例。您不需要再次创建片段实例。
-
@Piyush 先生,我是安卓新手。能否请您编写代码行以访问片段姿态?