【问题标题】:how to add the data of all the tabs into database using a single button click in android如何在android中单击一个按钮将所有选项卡的数据添加到数据库中
【发布时间】:2012-12-02 07:29:01
【问题描述】:

有五个 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>

【问题讨论】:

  • 嘿,您在哪里将 EditTexts 添加为 TabContent?它是您要膨胀和添加的另一个 LinearLayout,还是子活动或片段?
  • @Nagaraj436:是的,EditTexts 在另一个活动中。当用户单击选项卡时,它的选项卡内容会从另一个活动中调用。代码如下:// Tab for Song Details TabSpec DD = tabHost.newTabSpec("songs Details"); DD.setIndicator("songs details", getResources().getDrawable(R.drawable.dd)); Intent dd = new Intent(this, DropDetails.class); DD.setContent(dd);
  • 希望不可能,如果需要可以给出另一种解决方案
  • 是的...请先生...有什么可以替代的解决方案..
  • 我不想在 tabcontent 的每个活动结束时保留一个 Button。希望你能理解。

标签: android android-tabhost android-tabs android-tabactivity


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我已经夸大了三个活动。所有这三个活动都包含 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 输出如下:

    【讨论】:

      猜你喜欢
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2013-11-14
      • 1970-01-01
      相关资源
      最近更新 更多