使用Tab活动Android调用服务

时间:2011-02-15 07:38:32

标签: android

我对标签活动有点困惑,我在主视图上有3个标签,我可以使用标签切换视图,例如主要到课程或课程到主页,

但我想为每个标签调用不同的服务,例如

  1. 标签1(通话):当用户点击它时,它会拨打电话号码“88888888888”
  2. 标签2(电子邮件):当用户点击它时,用户将直接收到“撰写新电子邮件”并传递“aaaaaaa@aaa.com”之类的值
  3. Tab 3(Web):当用户点击它时,它将打开一个带有“www.helpme.co.uk”的浏览器
  4. 我目前的java和xml文件编码如下,

    Java代码:

    package com.NVT.android;
    
    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TabHost;
    
    public class MainTabActivity extends TabActivity{
    
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.tab_activity_layout);
    
                Resources res = getResources(); // Resource object to get Drawables
                TabHost tabHost = getTabHost();  // The activity TabHost
                TabHost.TabSpec spec;  // Resusable TabSpec for each tab
                Intent intent;  // Reusable Intent for each tab
    
                // Create an Intent to launch an Activity for the tab (to be reused)
                intent = new Intent().setClass(this, Main.class);
    
                // Initialize a TabSpec for each tab and add it to the TabHost
                spec = tabHost.newTabSpec("main").setIndicator("Main",
                                  res.getDrawable(R.drawable.ic_tab_artists_grey))
                              .setContent(intent);
                tabHost.addTab(spec);
    
                // Do the same for the other tabs
                intent = new Intent().setClass(this, Courses.class);
                spec = tabHost.newTabSpec("courses").setIndicator("Courses",
                                  res.getDrawable(R.drawable.ic_tab_artists_white))//m using same buttons for each tab u can change it by adding new drawable xml.. got that
                              .setContent(intent);
                tabHost.addTab(spec);
    
                intent = new Intent().setClass(this, Courses.class);
                spec = tabHost.newTabSpec("courses").setIndicator("Courses",
                                  res.getDrawable(R.drawable.ic_tab_artists_white))//m using same buttons for each tab u can change it by adding new drawable xml.. got that
                              .setContent(intent);
                tabHost.addTab(spec);
    
    
    
            }
        }
    

    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">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                 />
                      <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp"></FrameLayout>
    
        </RelativeLayout>
        </LinearLayout>
    </TabHost>
    

0 个答案:

没有答案