码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-09-24 21:31:27      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   io   os   java   ar   div   sp   

问题:当你的导航在TabActivity中 而子页面的一个button须要切换到当中的某一个导航页面

bubuko.com,布布扣bubuko.com,布布扣

转载请注明出处: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);
			}
		});
	}

}


 


 

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

标签:android   blog   http   io   os   java   ar   div   sp   

原文地址:http://www.cnblogs.com/mengfanrong/p/3991304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!