新标签和新活动可能吗?

时间:2013-04-16 21:12:12

标签: android android-activity tabs

我有三个标签(Tab1,标签2,标签3)。我想要启动所有标签的其他活动。 我在Tab 2中尝试了新的意图。

我的代码

  public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
            tabHost.setup();




                    TabSpec spec1=tabHost.newTabSpec("Tab 1");
            spec1.setContent(R.id.tab1);
            spec1.setIndicator("Tab 1");



            TabSpec spec2=tabHost.newTabSpec("Tab 2");
            spec2.setIndicator("Tab 2", getResources().getDrawable(R.drawable.ic_launcher));        
            spec2.setContent(new Intent(this,teszt.class));


            TabSpec spec3=tabHost.newTabSpec("Tab 3");
            spec3.setIndicator("Tab 3");
            spec3.setContent(R.id.tab3);

            tabHost.addTab(spec1);
            tabHost.addTab(spec2);
            tabHost.addTab(spec3);
            }

    }

的Manifest.xml

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="hu.anzy.fulek.example.fulek.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".teszt"
            ></activity>
    </application>

Teszt.java

public class teszt extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.teszt_layout);

}

public void onClick(View v){
    if (v.getId()== R.id.btnBackTo1){
        Toast.makeText(this, "Click BackTo1", Toast.LENGTH_LONG).show();
    } else
    {
        if (v.getId()== R.id.btnStart3) {
            Toast.makeText(this, "Click Start3", Toast.LENGTH_LONG).show();

        }
    }


}

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>

    <TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabHost"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <TabWidget
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/tabs"
    />
     <FrameLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabcontent"
     >
     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tab1"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="100px" 
    android:text="This is tab1"
    android:id="@+id/txt1"
    />    

     </LinearLayout>

     <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab2"
    android:orientation="vertical"
    android:paddingTop="60px"
     >


     </LinearLayout>

      <LinearLayout
     android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab3"
    android:orientation="vertical"
    android:paddingTop="60px"
     >
     <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="100px" 
    android:text="This is tab 3"
    android:id="@+id/txt3"
    />

     </LinearLayout>
     </FrameLayout>

    </TabHost>

Test_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

       <Button
        android:id="@+id/btnBackTo1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Back to 1st Activity"
        android:onClick="onClick"
     />
     <Button
          android:id="@+id/btnStart3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start 3rd Activity"
        android:onClick="onClick"
     />

</LinearLayout>

我的应用程序正在运行,但是当我选择了Tab 2时,应用程序停止了。

有什么问题?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。

   public class MainActivity extends TabActivity{

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TabHost tabHost = getTabHost();

            // Tab1
            TabSpec spec1 = tabHost.newTabSpec("Tab1");

            photospec.setIndicator("Tab1");
            Intent aIntent = new Intent(this, aActivity.class);
            photospec.setContent(aIntent);

            // Tab2
            TabSpec spec2 = tabHost.newTabSpec("Tab2");
            songspec.setIndicator("Tab2");
            Intent bIntent = new Intent(this, bActivity.class);
            songspec.setContent(bIntent);

            // Tab3
            TabSpec spec3 = tabHost.newTabSpec("Tab3");
            spec3.setIndicator("Tab3");
            Intent cIntent = new Intent(this, cActivity.class);
            spec3.setContent(cIntent);

            // Adding all TabSpec to TabHost
            tabHost.addTab(spec1); // Adding tab
            tabHost.addTab(spec2); // Adding tab
            tabHost.addTab(spec3); // Adding tab
        }
    }

此aActivity类似bActivity和cActivity

import android.app.Activity;
import android.os.Bundle;

public class aActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_layout);
    }
}

A_layout.xml类似于B_layout.xml和C_layout.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">


      <TextView android:text=" Text"
                android:padding="15dip"
                android:textSize="18dip"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>

main.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">
            <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="fill_parent"/>
        </LinearLayout>
    </TabHost>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hu.anzy.example.androidtablayoutactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="hu.anzy.example.MaintActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity android:name=".aActivity" />


        <activity android:name=".bActivity" />


        <activity android:name=".cActivity" />
    </application>

</manifest>