想要使用标签主机让我的页面更灵活..这是我的情况 例如,我有学生信息表格,有足够可控制的控件,所以想在选项卡中划分页面。
以前它只在一个页面中;在一个活动中有代码并单击提交按钮会将页面保存在一个名为studentTbl.sqlite的数据库表中
现在想要使用3个标签,每个标签带有下一个按钮,结束标签将有提交按钮,这将保存所有3个标签数据,保存点击studentTbl.sqlite
那么如何为单表保存实现这种单页活动的标签主机?
答案 0 :(得分:2)
创建一个新活动 Const.java 并添加以下代码。
public class Const {
public static String name;
public static String age;
public static String dob;
public static String schoolname;
public static String std;
public static String sub;
...
...
}
现在在第一个活动中,下一个按钮点击事件使用下面的代码。
next.setOnClickListener(new ImageView.OnClickListener()
{
public void onClick(View v)
{
Code.name= editTextName.getText().toString().trim();
Code.age= editTextAge.getText().toString().trim();
.......
.......
}
});
然后对所有活动(第二个活动)都相同,如果你想添加更多屏幕,那么在最后一个屏幕的提交按钮中获取这个全局变量值并将其传递给数据库。
希望它会对你有所帮助。如果你有任何问题,请让我知道......
答案 1 :(得分:0)
此示例代码可以帮助您......
public class SecondActivity extends FragmentActivity implements
ActionBar.TabListener {
private static final int CONTENT_ID = 0xFEED;
private Fragment tab1Fragment;
private Fragment tab2Fragment;
private Fragment tab3Fragment;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(getContentView());
init();
}
private void init() {
tab1Fragment = new Tab1Fragment();
tab2Fragment = new Tab2Fragment();
tab3Fragment = new Tab3Fragment();
initActionBar();
}
private void initActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Tab1")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab2")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Tab3")
.setTabListener(this));
}
private View getContentView() {
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setId(CONTENT_ID);
return frameLayout;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
ft.replace(CONTENT_ID, tab1Fragment);
break;
case 1:
ft.replace(CONTENT_ID, tab2Fragment);
break;
case 2:
ft.replace(CONTENT_ID, tab3Fragment);
break;
default:
break;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public static class StudentInfo {
public static String NAME;
public static String LOCATION;
public static String SCHOOL_NAME;
public static String COLLEGE_NAME;
public static String COMPANY;
public static String JOB;
}
public static abstract class BaseFragment extends Fragment implements
View.OnClickListener {
protected EditText editText1;
protected EditText editText2;
protected Button nextButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.second_activity_layout, container,
false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editText1 = (EditText) view.findViewById(R.id.editText1);
editText2 = (EditText) view.findViewById(R.id.editText2);
nextButton = (Button) view.findViewById(R.id.next);
nextButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
loadValues();
}
protected abstract void loadValues();
}
public static class Tab1Fragment extends BaseFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("Name");
textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("Location");
}
@Override
protected void loadValues() {
StudentInfo.NAME = editText1.getText().toString();
StudentInfo.LOCATION = editText2.getText().toString();
}
}
public static class Tab2Fragment extends BaseFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("School");
textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("College");
}
@Override
protected void loadValues() {
StudentInfo.SCHOOL_NAME = editText1.getText().toString();
StudentInfo.COLLEGE_NAME = editText2.getText().toString();
}
}
public static class Tab3Fragment extends BaseFragment {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText("Company");
textView = (TextView) view.findViewById(R.id.textView2);
textView.setText("Job");
}
@Override
protected void loadValues() {
StudentInfo.COMPANY = editText1.getText().toString();
StudentInfo.JOB = editText2.getText().toString();
}
}
}
这是second_activity_layout.xml ...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Label"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Label"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
</LinearLayout>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/linearLayout2"
android:layout_marginRight="25dp"
android:layout_marginTop="15dp"
android:text="Next"
tools:ignore="HardcodedText" />
</RelativeLayout>