2

有五个 EditText 小部件放置在第一个选项卡中,五个 EditText 小部件放置在第二个选项卡中,五个 EditText 小部件放置在第三个选项卡中。现在我想通过单击一个按钮将所有选项卡的数据添加到数据库中。该按钮不在选项卡布局内。它在线性布局内...... xml树结构是这样的:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="300dp" >
        </FrameLayout>
                 <Button
                     android:id="@+id/submit"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     android:text="Submit" />
    </LinearLayout>
</TabHost>
4

2 回答 2

1

是的,不要在 tabcontent 的每个活动的末尾放置一个按钮。您所做的不是参加 3 个单独的活动,而是不参加任何内部子活动。在您的主要活动中,膨胀所有布局(您将其设置为活动的内容视图)并将它们设置为 3 个选项卡的内容。然后你将有 3 个被膨胀的视图。现在创建关于视图透视的 EditTexts 对象,并在单击按钮时使用它们。希望你能理解我的想法。更重要的是,不建议将活动用作选项卡的内容,因为它们会消耗更多内存。我知道有很多教程都遵循相同但隐藏了缺点。

于 2012-12-03T14:45:50.703 回答
0

我夸大了三个活动。所有这三个活动都包含 EditTexts。代码如下:

    TabHost tabHost = getTabHost();

    // Tab for Photos
    TabSpec photospec = tabHost.newTabSpec("Photos");
    photospec.setIndicator("General", getResources().getDrawable(R.drawable.icon_photos_tab));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

    // Tab for Songs
    TabSpec songspec = tabHost.newTabSpec("Songs");
    // setting Title and Icon for the Tab
    songspec.setIndicator("Private", getResources().getDrawable(R.drawable.icon_songs_tab));
    Intent songsIntent = new Intent(this, SongsActivity.class);
    songspec.setContent(songsIntent);

    // Tab for Videos
    TabSpec videospec = tabHost.newTabSpec("Videos");
    videospec.setIndicator("Other", getResources().getDrawable(R.drawable.icon_videos_tab));
    Intent videosIntent = new Intent(this, VideosActivity.class);
    videospec.setContent(videosIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab
    tabHost.addTab(videospec); // Adding videos tab

我已经在所有 3 个选项卡中添加了 EditText,我想通过单击一个按钮将所有 3 个选项卡(包含 EditText)的数据添加到数据库中。并且按钮在 main.xml 中输出如下:

输出看起来像这样

于 2012-12-22T09:25:42.077 回答