我不知道您要验证的信息的性质,但您可以使用包含来自片段 1 和片段 2 的信息的 Singleton 类,当您想提交表单时,您可以简单地从中获取信息类并验证它。
编辑:
假设我们有 FragmentA、FragmentB 和一个 Singleton DataBuilder,其中包含来自两个 Fragment 的数据。在 FragmentA 中,您可以将所有数据保存到 DataBuilder 中并将用户导航到 FragmentB。然后在提交数据之前,您必须对其进行验证。如果 FragmentA 的验证为假,只需将用户导航回 FragmentA 并显示一条消息。
片段A:
package aero.djetops;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FragmentA extends Fragment{
private EditText firstInput;
private EditText secondInput;
private EditText thirdInput;
private Button bContinue;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentA_layout, container, false);
firstInput = (EditText) rootView.findViewById(R.id.first_input);
secondInput = (EditText) rootView.findViewById(R.id.second_input);
thirdInput = (EditText) rootView.findViewById(R.id.third_input);
// populate the inputs with the data from DataBuilder
firstInput.setText(DataBuilder.getInstance().getFragmentA_firstInputData());
secondInput.setText(DataBuilder.getInstance().getFragmentA_secondInputData());
thirdInput.setText(DataBuilder.getInstance().getFragmentA_thirdInputData());
bContinue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBuilder.getInstance().setFragmentA_firstInputData(firstInput.getText().toString());
DataBuilder.getInstance().setFragmentA_secondInputData(secondInput.getText().toString());
DataBuilder.getInstance().setFragmentB_thirdInputData(thirdInput.getText().toString());
FragmentB fragmentB = new FragmentB();
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentB)
.addToBackStack(null).commit();
}
});
return rootView;
}
}
片段B:
package aero.djetops;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class FragmentB extends Fragment{
private EditText firstInput;
private EditText secondInput;
private EditText thirdInput;
private Button bSubmit;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentB_layout, container, false);
firstInput = (EditText) rootView.findViewById(R.id.first_input);
secondInput = (EditText) rootView.findViewById(R.id.second_input);
thirdInput = (EditText) rootView.findViewById(R.id.third_input);
// populate the inputs with the data from DataBuilder
firstInput.setText(DataBuilder.getInstance().getFragmentB_firstInputData());
secondInput.setText(DataBuilder.getInstance().getFragmentB_secondInputData());
thirdInput.setText(DataBuilder.getInstance().getFragmentB_thirdInputData());
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBuilder.getInstance().setFragmentB_firstInputData(firstInput.getText().toString());
DataBuilder.getInstance().setFragmentB_secondInputData(secondInput.getText().toString());
DataBuilder.getInstance().setFragmentB_thirdInputData(thirdInput.getText().toString());
if(validateDataFragmentA()) {
showErrorMessage();
FragmentA fragmentA = new FragmentA();
getFragmentManager().beginTransaction().replace(R.id.content_frame, fragmentA)
.addToBackStack(null).commit();
return;
} else if(validateDataFragmentB()) {
showErrorMessage();
return;
}
// everything is ok
// submit data here
}
});
return rootView;
}
}
数据构建器:
package aero.djetops;
public class DataBuilder {
private static DataBuilder ourInstance = new DataBuilder();
public static DataBuilder getInstance() {
return ourInstance;
}
// fragment A
private String fragmentA_firstInputData;
private String fragmentA_secondInputData;
private String fragmentA_thirdInputData;
// fragment B
private String fragmentB_firstInputData;
private String fragmentB_secondInputData;
private String fragmentB_thirdInputData;
private DataBuilder() {
fragmentA_firstInputData = "";
fragmentA_secondInputData = "";
fragmentA_thirdInputData = "";
fragmentB_firstInputData = "";
fragmentB_secondInputData = "";
fragmentB_thirdInputData = "";
}
/**
* Clear the data in the builder
*/
public void clearInputData() {
fragmentA_firstInputData = "";
fragmentA_secondInputData = "";
fragmentA_thirdInputData = "";
fragmentB_firstInputData = "";
fragmentB_secondInputData = "";
fragmentB_thirdInputData = "";
}
// GETTERS and SETTERS
public String getFragmentA_firstInputData() {
return fragmentA_firstInputData;
}
public void setFragmentA_firstInputData(String fragmentA_firstInputData) {
this.fragmentA_firstInputData = fragmentA_firstInputData;
}
public String getFragmentA_secondInputData() {
return fragmentA_secondInputData;
}
public void setFragmentA_secondInputData(String fragmentA_secondInputData) {
this.fragmentA_secondInputData = fragmentA_secondInputData;
}
public String getFragmentA_thirdInputData() {
return fragmentA_thirdInputData;
}
public void setFragmentA_thirdInputData(String fragmentA_thirdInputData) {
this.fragmentA_thirdInputData = fragmentA_thirdInputData;
}
public String getFragmentB_firstInputData() {
return fragmentB_firstInputData;
}
public void setFragmentB_firstInputData(String fragmentB_firstInputData) {
this.fragmentB_firstInputData = fragmentB_firstInputData;
}
public String getFragmentB_secondInputData() {
return fragmentB_secondInputData;
}
public void setFragmentB_secondInputData(String fragmentB_secondInputData) {
this.fragmentB_secondInputData = fragmentB_secondInputData;
}
public String getFragmentB_thirdInputData() {
return fragmentB_thirdInputData;
}
public void setFragmentB_thirdInputData(String fragmentB_thirdInputData) {
this.fragmentB_thirdInputData = fragmentB_thirdInputData;
}
}