我想将表单分成2个选项卡,第二个选项卡包含 确认底部应检查所需字段是否为 填写并继续将其提交给服务器。
部分必填字段位于片段1(标签1)和我的按钮中 onclick函数位于片段2(tab2)上。
如何将EditText视图组件从片段1 xml传递给 片段2 java类。
或者我应该将所有功能都放在主要活动中吗?
主要活动
$(submitInput).after("<div className=\"alert alert-danger\" role=\"alert\">Wrong Phone Format</div>");
Fragment1 xml
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_company);
final FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//1
tabHost.addTab(tabHost.newTabSpec("Basic Information")
.setIndicator("Basic Information"),
BasicCompanyInformation.class,
null);
//2
tabHost.addTab(tabHost.newTabSpec("Contact")
.setIndicator("Contact"),
ContactCompany.class,
null);
}
//Fragment 1
public class ContactCompany extends Fragment {
Button btnSubmit;
EditText companyName;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
CreateCompanyActivity createCompanyActivity = (CreateCompanyActivity) activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View basicCompanyView = inflater.inflate(R.layout.basic_company_information, container,
false);
companyName = (EditText) basicCompanyView.findViewById(R.id.edtTxtCompName);
return inflater.inflate(R.layout.contact_company, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnSubmit = (Button) this.getView().findViewById(R.id.btnSave);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String companyNameValue = companyName.getText().toString();
btnSubmit.setText(companyNameValue);
}
});
}
}
//Fragment 2
public class BasicCompanyInformation extends Fragment {
private String[] statusArray;
private Spinner statusSpinner;
private Button btnNext1;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
CreateCompanyActivity createCompanyActivity = (CreateCompanyActivity) activity;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
statusArray = new String[] {
"Call", "Following", "Deal", "Give up"
};
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.basic_company_information, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
statusSpinner = (Spinner)getView().findViewById(R.id.statusSpinner);
btnNext1 = (Button)getView().findViewById(R.id.btnNext1);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity().getApplicationContext(),
R.layout.drop_down, statusArray);
statusSpinner.setAdapter(adapter);
btnNext1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TabHost tabHost = (TabHost) getActivity().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
}
});
}
}
片段2 xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f4f4f4">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#fff"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name:"
android:id="@+id/txtCompName"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtTxtCompName"
android:hint="Company Name"
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:inputType="textNoSuggestions"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Status:"
android:id="@+id/txtStatus"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/statusSpinner"
android:layout_column="1"
android:layout_marginRight="5dp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Reg No:"
android:id="@+id/txtRegNo"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtTxtRegNo"
android:hint="Register No."
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:inputType="textNoSuggestions"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Type:"
android:id="@+id/txtBusinessType"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edttxtBusinessType"
android:hint="Business Type"
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:inputType="textNoSuggestions"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Address:"
android:id="@+id/txtAddress"
android:textStyle="bold"
android:layout_gravity="center|top"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="top"
android:lines="5"
android:ems="10"
android:scrollbars="vertical"
android:id="@+id/edtTxtAddress"
android:hint="Address"
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:maxLines="5"/>
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnNext1"
android:layout_marginTop="5dp"/>
</RelativeLayout>
</TableLayout>
</ScrollView>> </LinearLayout>
答案 0 :(得分:0)
在你的主片段(一个持有视图分页器,标签布局)中保留两个片段的引用(你只需要fisrt,但万一以后你可能还需要第二个)。像这样:
v
在您的片段2中,您想要引用它并检查一些结果,请执行以下操作: 当您在持有mainfragment的ur活动中执行事务时,请确保已为主片段设置了标记。这样你就可以使用标签来引用主片段。
private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
@Override
public Object instantiateItem(ViewGroup container, int position) {
Log.d("dj", "intializing items FragPageAdapter");
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Log.d("dj", "destroyed items FragPageAdapter");
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int fragmentPosition) {
return registeredFragments.get(fragmentPosition);
}
在第二个片段中获取第一个片段的引用,如下所示:
mainFragment= (MainFragment) getActivity().getSupportFragmentManager().
findFragmentByTag(ActivityHoldingMainFragment.TAG_FOR_MAIN_FRAG);
// findFragmentByPosition(0)将持有第一个片段;
在第一个片段中创建一个获取所有数据的方法;像这样:
public Fragment findFragmentByPosition(int fragmentPosition) {
return mainFragment.getRegisteredFragment(fragmentPosition);
}
FirstFragment firstFrag = (FirstFragment ) findFragmentByPosition(0);
ArrayList<String> formOneData = firstFrag.getFormOneData();