我有一个实现 TabActivity 的应用程序,但问题是当我单击后退按钮打开活动后,应用程序无法关闭,
我怎样才能完成这个 TabActivity ?
MainActivity.java
包 com.productdemo;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class MainActivity extends TabActivity {
TabHost tabhost;
TabSpec dashboard, product, customers, order, settings;
public final static int DASHBOARD = 1;
public final static int PRODUCT = 2;
public final static int CUSTOMER = 3;
public final static int ORDER = 4;
public final static int SETTINGS = 5;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, TabGroup1Activity.class);
spec = tabhost.newTabSpec("Dashboard").setIndicator("Dashboard")
.setContent(intent);
tabhost.addTab(spec);
intent = new Intent().setClass(this, TabGroup2Activity.class);
spec = tabhost.newTabSpec("Product").setIndicator("Product")
.setContent(intent);
tabhost.addTab(spec);
intent = new Intent().setClass(this, TabGroup3Activity.class);
spec = tabhost.newTabSpec("Customer").setIndicator("Customer")
.setContent(intent);
tabhost.addTab(spec);
intent = new Intent().setClass(this, TabGroup4Activity.class);
spec = tabhost.newTabSpec("Order").setIndicator("Order")
.setContent(intent);
tabhost.addTab(spec);
intent = new Intent().setClass(this, TabGroup5Activity.class);
spec = tabhost.newTabSpec("Settings").setIndicator("Settings")
.setContent(intent);
tabhost.addTab(spec);
tabhost.setCurrentTab(1);
int type = 0;
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey("from")) {
type = getIntent().getExtras().getInt("from");
switch (type) {
case DASHBOARD:
tabhost.setCurrentTab(0);
case PRODUCT:
tabhost.setCurrentTab(1);
case CUSTOMER:
tabhost.setCurrentTab(2);
case ORDER:
tabhost.setCurrentTab(3);
case SETTINGS:
tabhost.setCurrentTab(4);
default:
tabhost.setCurrentTab(0);
}
}
}
}
public void switchTabSpecial(int tab) {
tabhost.setCurrentTab(tab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class MyView extends LinearLayout {
ImageView iv;
public MyView(Context c, int drawable, int drawableselec, String label) {
super(c);
iv = new ImageView(c);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(SELECTED_STATE_SET, this.getResources()
.getDrawable(drawable));
listDrawable.addState(ENABLED_STATE_SET, this.getResources()
.getDrawable(drawableselec));
iv.setImageDrawable(listDrawable);
iv.setBackgroundColor(Color.TRANSPARENT);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (float) 0.0));
iv.setPadding(0, 0, 0, 5);
setGravity(Gravity.CENTER);
addView(iv);
}
}
@Override
public void onBackPressed() {
this.finish();
}
}
activity_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="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="0dp" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:padding="0dp" > </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0" /> <TabWidget android:id="@+id/tabwidget" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0" /> </LinearLayout>
提前致谢...