0

我正在尝试将从所有 EditText 输入的所有信息保存在我的 3 个片段中的每个片段中。我正在尝试从我的 ActionBar 中的选项按钮获取这些信息。

我注意到当我在不同的选项卡上时它找不到视图。

我的第一个片段/第一个标签:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="gen_info">

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Admin Account"
        android:id="@+id/isAdmin" />

    <EditText
        android:id="@+id/code"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:hint="@string/code"
        android:ems="10"
        android:width="150dip" />

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:hint="@string/fullname" />

    <EditText
        android:id="@+id/address"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:hint="@string/address"
        android:layout_marginTop="5dip" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/birthdate"
        android:hint="Birthdate (mm/dd/yyy)" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/mobile"
        android:hint="Mobile No" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:layout_marginTop="5dip"
        android:ems="10"
        android:id="@+id/workPhone"
        android:hint="Work Phone" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/email"
        android:hint="Email" />

        <Spinner
            android:id="@+id/gender_options"
            android:layout_marginTop="5dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <Spinner
        android:id="@+id/civil_status"
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:layout_marginTop="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/hired_date"
        android:hint="Hired Date (mm/dd/yyyy)" />

    <EditText
        android:id="@+id/location"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dip"
        android:singleLine="true"
        android:hint="Location" />

    <Spinner
        android:id="@+id/position"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Spinner
        android:id="@+id/department"
        android:layout_marginTop="5dip"
        android:layout_marginBottom="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

我的第二个片段/标签

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="contacts">

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/contactName"
        android:hint="Name"
        android:paddingTop="5dip" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/contactRelationship"
        android:hint="Relationship"
        android:paddingTop="5dip" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:ems="10"
        android:id="@+id/contactAddress"
        android:hint="Address"
        android:paddingTop="5dip" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/contactPhoneNo"
            android:hint="Phone Number"
            android:paddingTop="5dip" />

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/contactPhoneTypeSpinner"
            android:layout_toEndOf="@+id/contactPhoneNo"
            android:layout_toRightOf="@+id/contactPhoneNo"
            android:paddingTop="5dip" />
    </RelativeLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="107dp"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/contactSpecialNotes"
        android:paddingTop="5dip"
        android:hint="Write special notes here" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add to list"
        android:id="@+id/addContact" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Contacts List"
        android:id="@+id/contactsHeader"
        android:editable="false" />

    <TableLayout
        android:id="@+id/contactsTableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:paddingTop="5dip"></TableLayout>
</LinearLayout>

我的 3rdfragment / 标签:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="latest_wage">

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical">


    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/latestWageDate"
        android:hint="Date" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/latestWageNote"
        android:hint="Note" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/latestWageRate"
        android:hint="Rate (PHP)" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Rate"
        android:id="@+id/btnAddRate" />

    <TableLayout
        android:id="@+id/latestWageTableLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="5dip"></TableLayout>
</LinearLayout>

我在这里声明了我的第一个 Fragment 的元素:

public static class LaunchpadSectionFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);

        Spinner spinner = (Spinner) rootView.findViewById(R.id.gender_options);
        Spinner civilStatusSpinner = (Spinner) rootView.findViewById(R.id.civil_status);
        Spinner positionSpinner = (Spinner) rootView.findViewById(R.id.position);
        Spinner departmentSpinner = (Spinner) rootView.findViewById(R.id.department);

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }
}

这是我的 ActionBar 的功能:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        case R.id.action_save:

            EditText code = (EditText) findViewById(R.id.code);
            EditText name = (EditText) findViewById(R.id.name);
            EditText address = (EditText) findViewById(R.id.address);
            EditText birthDate = (EditText) findViewById(R.id.birthdate);
            EditText mobile = (EditText) findViewById(R.id.mobile);
            EditText workPhone = (EditText) findViewById(R.id.workPhone);
            EditText email = (EditText) findViewById(R.id.email);
            Spinner gender = (Spinner) findViewById(R.id.gender_options);
            Spinner civilStatus = (Spinner) findViewById(R.id.civil_status);
            EditText hiredDate = (EditText) findViewById(R.id.hired_date);
            EditText location = (EditText) findViewById(R.id.location);
            Spinner departmentId = (Spinner) findViewById(R.id.department);
            CheckBox isAdmin = (CheckBox) findViewById(R.id.isAdmin);

            return true;
        case R.id.action_discard:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

所有视图都将返回 null,这些视图在第一个片段中找到。当我没有制作lat两个片段时,我可以找到视图。

任何帮助将不胜感激。谢谢!

4

2 回答 2

0

尝试给定的代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        case R.id.action_save:

            EditText code = (EditText) getView().findViewById(R.id.code);
            EditText name = (EditText) getView().findViewById(R.id.name);
            EditText address = (EditText) getView().findViewById(R.id.address);
            EditText birthDate = (EditText) getView().findViewById(R.id.birthdate);
            EditText mobile = (EditText) getView().findViewById(R.id.mobile);
            EditText workPhone = (EditText) getView().findViewById(R.id.workPhone);
            EditText email = (EditText) getView().findViewById(R.id.email);
            Spinner gender = (Spinner) getView().findViewById(R.id.gender_options);
            Spinner civilStatus = (Spinner) getView().findViewById(R.id.civil_status);
            EditText hiredDate = (EditText) getView().findViewById(R.id.hired_date);
            EditText location = (EditText) getView().getView().findViewById(R.id.location);
            Spinner departmentId = (Spinner) getView().findViewById(R.id.department);
            CheckBox isAdmin = (CheckBox) getView().findViewById(R.id.isAdmin);

            return true;
        case R.id.action_discard:
            this.finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
于 2013-10-04T09:21:45.640 回答
0

方法可能是在我们的 Fragment 本身中处理您的 itemMenuClick。因此,您可以在 Fragment 中处理它们并获取所需的信息:TUTORIAL

另一种方法是在您的 MainActivity 中全局保留所有 Fragment 的实例,并向您的 Fragment 询问值,例如:

MainActivity:

Fragment1 _fragment1;
onCreate(..){
   _fragment1 = new Fragment1();
}

onMenuItemSelected(....){

   String test = _fragment1.getCustomText();
}


Fragment1:

EditText _test;

onCreate(...){

  _test = _root.findViewById(R.id.et_test);
}


public String getCustomText(){
   return _test.getText().toString();
}
于 2013-10-04T09:58:50.247 回答