首页 > 代码库 > 解决TabActivity中子页面不通过导航跳转到另一个页面的问题

解决TabActivity中子页面不通过导航跳转到另一个页面的问题

问题:当你的导航在TabActivity中 而子页面的一个按钮需要切换到其中的某一个导航页面

转载请注明出处:http://blog.csdn.net/x605940745

demo下载地址:http://download.csdn.net/detail/x605940745/7353695

package com.example.shows;

import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;

/**
 * 解决TabActivity中子页面不通过导航跳转到另一个页面的问题
 * @author xionglh
 *
 */
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnClickListener {

	private Intent aIntent;
	private Intent bIntent;
	private TabHost host;
	private Button show1;
	private Button show2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		aIntent = new Intent(this, ActivityA.class);
		bIntent = new Intent(this, ActivityB.class);
		host = getTabHost();
		host.addTab(host.newTabSpec("a").setIndicator("a").setContent(aIntent));
		host.addTab(host.newTabSpec("b").setIndicator("b").setContent(bIntent));
		show1 = (Button) findViewById(R.id.bt_showa);
		show2 = (Button) findViewById(R.id.bt_showb);
		show1.setOnClickListener(this);
		show2.setOnClickListener(this);
		//注册广播
		IntentFilter filter = new IntentFilter("com.example.shows.shows");
		TestRevice revice = new TestRevice();
		registerReceiver(revice, filter);

	}

	class TestRevice extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			//实现跳转
			host.setCurrentTabByTag("a");
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_showa:
			host.setCurrentTabByTag("a");
			break;
		case R.id.bt_showb:
			host.setCurrentTabByTag("b");
		default:
			break;
		}
	}
}

 

package com.example.shows;

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

public class ActivityA extends Activity {

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

}


 

package com.example.shows;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ActivityB extends Activity {

	private Button show;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.b);
		show = (Button) findViewById(R.id.ss);
		show.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 发送广播
				Intent intent = new Intent("com.example.shows.shows");
				sendBroadcast(intent);
			}
		});
	}

}