Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-在选项卡式片段之间传递“编辑文本”元素值_Android_Android Fragments_Tabs_Android Edittext - Fatal编程技术网

Android-在选项卡式片段之间传递“编辑文本”元素值

Android-在选项卡式片段之间传递“编辑文本”元素值,android,android-fragments,tabs,android-edittext,Android,Android Fragments,Tabs,Android Edittext,我有一个包含多个滑动选项卡碎片的活动。每个片段都有一些复选框、编辑文本和切换字段。我想要的是浏览所有选项卡,编辑字段的状态,然后在最后一个选项卡上整理所有元素中的信息,并将这些详细信息保存到数据库中 我的问题是,我不知道如何保存这些元素值并将它们传递到我的最后一个选项卡以便存储 我是Android应用程序的新手,所以任何提示或回答都将非常感谢 以下是迄今为止代码的外观: 活动: 标签片段2 假设Tab Fragment 2是我的最后一个选项卡,我如何在这里检索存储在Tab Fragment 1中

我有一个包含多个滑动选项卡碎片的活动。每个片段都有一些复选框、编辑文本和切换字段。我想要的是浏览所有选项卡,编辑字段的状态,然后在最后一个选项卡上整理所有元素中的信息,并将这些详细信息保存到数据库中

我的问题是,我不知道如何保存这些元素值并将它们传递到我的最后一个选项卡以便存储

我是Android应用程序的新手,所以任何提示或回答都将非常感谢

以下是迄今为止代码的外观:

活动:

标签片段2

假设Tab Fragment 2是我的最后一个选项卡,我如何在这里检索存储在Tab Fragment 1中的mBreedName的文本值


谢谢

您可以将这些值存储在活动中,并使用回调来更新和查询它们

试试这个:

关于您的活动:

public class IconTextTabsActivity extends AppCompatActivity implements ActivityCallback{
    private String mBreedTextName = "";
    ...
    @Override
    public void onEditTextChange(String text) {
        this.mBreedTextName = text;
    }

    @Override
    public String getEditTextName() {
        return mBreedTextName;
    }
}
ActivityCallback接口:

public interface ActivityCallback {

    void onEditTextChange(String text);

    String getEditTextName();
}
然后在你的片段上:

public class OneFragment extends Fragment {
    ...
    private ActivityCallback callback;    

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_one, container, false);
    mBreedName = (EditText) v.findViewById(R.id.addName);
    Button b = v.findViewById(R.id.accept_button);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                callback.onEditTextChange(mBreedName.getText().toString());
            }
        });
        return v;
    }

}
public class TwoFragment extends Fragment {

    ...
    private ActivityCallback callback;    
    private String text;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_two, container, false);
        text = callback.getEditTextName();
        return v;
    }
}
第二个片段也是一样的:

public class OneFragment extends Fragment {
    ...
    private ActivityCallback callback;    

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_one, container, false);
    mBreedName = (EditText) v.findViewById(R.id.addName);
    Button b = v.findViewById(R.id.accept_button);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                callback.onEditTextChange(mBreedName.getText().toString());
            }
        });
        return v;
    }

}
public class TwoFragment extends Fragment {

    ...
    private ActivityCallback callback;    
    private String text;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_two, container, false);
        text = callback.getEditTextName();
        return v;
    }
}

还有一个问题,是否可以避免添加该按钮并附加OnSwipeTouchListener以触发回调?也许,我从未使用过该侦听器,但我猜这是可能的,也许您可以在onPause fragment方法中触发回调,每次你留下片段时都会调用它,例如当你滑动寻呼机时。如果答案解决了你的问题,请将其标记为正确!谢谢再次感谢!不客气,你能实现侦听器吗?
public class TwoFragment extends Fragment {

    ...
    private ActivityCallback callback;    
    private String text;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        callback = (ActivityCallback) context;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_two, container, false);
        text = callback.getEditTextName();
        return v;
    }
}