1
我想将表单分隔成2个选项卡,第二个选项卡包含 一个确认底部,该底部应检查是否填写了所需的字段并将其继续提交给服务器。FragmentTabHost将片段从片段传递到另一片段
某些必填字段位于片段1(选项卡1)中,而我的按钮 onclick函数位于片段2(选项卡2)上。
如何将EditText视图组件从片段1 xml传递到 片段2 java类。
或者我应该把所有的功能放在主要活动内?
主要活动
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);
}
});
}
}
片段1的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>
片段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">
<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="Person:"
android:id="@+id/txtPerson"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTxtPerson"
android:hint="Person in charge"
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="E-mail:"
android:id="@+id/txtEmail"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editTxtEmail"
android:hint="E-mail address"
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:inputType="textEmailAddress"
android:singleLine="true"/>
</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="Tel:"
android:id="@+id/txtTel"
android:textStyle="bold"
android:layout_gravity="center|center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edtTxtTel"
android:hint="Telephone"
android:background="@drawable/border"
android:paddingLeft="5dp"
android:layout_marginRight="5dp"
android:singleLine="true"
android:inputType="phone"/>
</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="Remark"
android:id="@+id/txtRemark"
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/edtTxtRemark"
android:hint="Remark"
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="Confirm"
android:id="@+id/btnSave"
android:layout_marginTop="5dp"/>
</RelativeLayout>
</TableLayout>
</ScrollView>>
顺便说一句,我怎么能得到我的片段标签在另一个活动? –
在活动类中创建一个静态变量,您可以使用它并随后使用它。像这样....在你做事务的活动中: - **声明一个类级别的变量:public static final String TAG_FOR_MAIN_FRAG =“packageName.MainFragment”; 然后声明并初始化您的主要片段:MainFragment mainFrag = new MainFragment(); //稍后执行事务 getSupportFragmentManager()。beginTransaction()。replace(container.getId(),mainFrag,TAG_FOR_MAIN_FRAG).addToBackStack(TAG_FOR_MAIN_FRAG)。承诺(); ** //添加到后台是你的选择。你可以删除它也 – DJphy
然后在另一个活动引用你的mainFragment使用标签声明为公共静态在前面提到的活动。 – DJphy