0

如何使用 Button 的 onClickHandler 以最简单的方式切换到不同的页面(视图)?

这是主要活动:

package myapp.tabnavui;

import myapp.tabnavui.List1;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void startClickHandler(View v) {
    setContent(new Intent(this, Tabs3.class));
}

}

我想让 startClickHandler 使应用程序切换到 Tabs3:

package nl.computerhuys.tabnavui;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.content.Intent;

/**
 * An example of tab content that launches an activity via {@link android.widget.TabHost.TabSpec#setContent(android.content.Intent)}
 */
public class Tabs3 extends TabActivity {

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

        final TabHost tabHost = getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("list")
                .setContent(new Intent(this, List1.class)));

        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("photo list")
                .setContent(new Intent(this, List8.class)));

        // This tab sets the intent flag so that it is recreated each time
        // the tab is clicked.
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("destroy")
                .setContent(new Intent(this, Controls2.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
    }
}

我该怎么做(针对 Android 4,最低 Android 2.2)

4

1 回答 1

0

在这里找到它:在 Android 应用程序中使用 Intent 来显示另一个活动

代码:

public void startClickHandler(View v) {
  Intent intent = new Intent(MainActivity.this, Tabs3.class);
  startActivity(intent);
}
于 2012-12-12T14:19:59.333 回答