0

我有 MainActvity 类,其中包含一个 Tab(指标为 Tab)

这是我的代码:-

public class MainActivity extends TabActivity {
TabHost tabHost;
Context context = MainActivity.this;
TabSpec spec;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabHost tabHost = getTabHost();

    tabHost.addTab(tabHost
            .newTabSpec("Tab")
            .setIndicator("Tab")
            .setContent(
                    new Intent(this, MyActivityGroup.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

    tabHost.setCurrentTab(0);

}

}

这在内部调用 MyActvityGroup,它将 ActvityGroup 类扩展为。

public class MyActivityGroup extends ActivityGroup {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // you van get the local activitymanager to start the new activity

    View view = getLocalActivityManager().startActivity(
            "ReferenceName",
            new Intent(this, NewActivity.class)
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
            .getDecorView();
    this.setContentView(view);

}
}

在 NewActvity,java 文件中我做了类似的事情

public class NewActivity extends Activity {

Button btnGo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.new_activity);

    btnGo = (Button) findViewById(R.id.btnStartActivity);
    btnGo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(NewActivity.this, "Clicked", Toast.LENGTH_SHORT)
                    .show();

            Intent intent = new Intent();
            intent.setClass(getParent(), ButtonActivity.class);
            startActivity(intent);

        }
    });

}
}
  • 现在这里这个活动包含一个按钮。
  • 我想在此按钮的单击事件的同一选项卡区域中启动新活动,但它以全屏模式启动。我再也看不到我的标签了。
  • 如何在当前选项卡容器中启动活动。
  • 我的 ActvityGroup 课程有问题,或者我没有以正确的方式开始活动..?
4

4 回答 4

1

使用下面的代码而不是您的代码,它将解决您的问题。

MyActivityGroup.java

public class MyActivityGroup extends ActivityGroup {

    private Stack<String> stack;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (stack == null) {
            stack = new Stack<String>();
        }

        push("HomeStackActivity", new Intent(this, NewActivity.class));
    }

    @Override
    public void finishFromChild(Activity child) {
        pop();
    }

    @Override
    public void onBackPressed() {
        pop();
    }

    public void push(String id, Intent intent) {
        Window window = getLocalActivityManager().startActivity(id,
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        if (window != null) {
            stack.push(id);
            setContentView(window.getDecorView());
        }
    }

    public void pop() {
        if (stack.size() == 1) {
            finish();
        }
        LocalActivityManager manager = getLocalActivityManager();
        manager.destroyActivity(stack.pop(), true);
        if (stack.size() > 0) {
            Intent lastIntent = manager.getActivity(stack.peek()).getIntent()
                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Window newWindow = manager.startActivity(stack.peek(), lastIntent);
            setContentView(newWindow.getDecorView());
        }
    }
}

新活动.java

public class NewActivity extends Activity {

    Button btnGo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.new_activity);

        btnGo = (Button) findViewById(R.id.btnStartActivity);
        btnGo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(NewActivity.this, "Clicked", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(getParent(), ButtonActivity.class);
                MyActivityGroup activityStack = (MyActivityGroup) getParent();
                activityStack.push("Button Activity", intent);                    
            }
        });
    }
}

请参阅下面的链接了解更多信息。

TabActivity 中的多个 Android 活动

于 2012-11-22T09:10:13.647 回答
1
private Stack<String> stack;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  if (stack == null)
   stack = new Stack<String>();
  // start default activity
  push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
 }

 @Override
 public void finishFromChild(Activity child) {
  pop();
 }

 @Override
 public void onBackPressed() {
  pop();
 }

 public void push(String id, Intent intent) {
  Window window = getLocalActivityManager().startActivity(id,
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
  if (window != null) {
   stack.push(id);
   setContentView(window.getDecorView());
  }
 }

 public void pop() {
  if (stack.size() == 1)
   finish();
  LocalActivityManager manager = getLocalActivityManager();
  manager.destroyActivity(stack.pop(), true);
  if (stack.size() > 0) {
   Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
   Window newWindow = manager.startActivity(stack.peek(), lastIntent);
   setContentView(newWindow.getDecorView());
  }
 }
}


TabActivity.java

public class TabActivity extends TabActivity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tab_screen);
  TabHost tabHost = getTabHost();
  Intent intent = new Intent().setClass(this, ActivityStack.class);
  TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp",
    getResources().getDrawable(R.drawable.home));
  spec.setContent(intent);

  tabHost.addTab(spec);

  Intent intent1 = new Intent().setClass(this, ActivityStack.class);
  TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp",
    getResources().getDrawable(R.drawable.invoice));
  spec1.setContent(intent1);
  tabHost.addTab(spec1);

  tabHost.setCurrentTab(0);
 }
}


FirstActivity.java

public class FirstActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView textView = new TextView(this);
  textView.setText("Tab Sample Activity ");
  textView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(getParent(), SecondActivity.class);
    ActivityStack activityStack = (ActivityStack) getParent();
    activityStack.push("SecondActivity", intent);

   }
  });
  setContentView(textView);
 }
}


SecondActivity.java

public class SecondActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView textView = new TextView(this);
  textView.setText("First Stack Activity ");
  textView.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(getParent(), ThirdActivity.class);
    ActivityStack activityStack = (ActivityStack) getParent();
    activityStack.push("ThirdActivity", intent);

   }
  });
  setContentView(textView);
 }
}


ThirdActivity.java
public class ThirdActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
}


Add Below XML files into your res/layout folder.

1) tab_screen.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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="3dp" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</TabHost>


2) main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>


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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            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=".ActivityStack"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
于 2012-11-22T10:09:59.587 回答
0

ActivityGroup 类已被弃用,它会导致性能不佳。最好使用 Fragments 来实现相同的目标。请参考以下链接

于 2012-11-22T09:03:59.010 回答
0

使用片段而不是活动。在 TabHost 中使用一些 FragmentActivity,然后按按钮替换片段。

于 2012-11-22T09:06:25.240 回答