Android TabHost中Activity之间传递数据

例子1:

 TabHost tabhost = (TabHost) findViewById(android.R.id.tabhost);
        tabhost.setup(this.getLocalActivityManager());
        Intent intent1 = new Intent(this,Second.class);
        Bundle bundle1 = new Bundle();
        bundle1.putStringArray("string", strings1);
        bundle1.putFloatArray("values", values1);
        intent1.putExtra("bundle",bundle1);
        
        Intent intent2 = new Intent(this,Second.class);
        Bundle bundle2 = new Bundle();
        bundle2.putStringArray("string", strings2);
        bundle2.putFloatArray("values", values2);
        intent2.putExtra("bundle",bundle2);
        
        Intent intent3 = new Intent(this,Second.class);
        Bundle bundle3 = new Bundle();
        bundle3.putStringArray("string", strings3);
        bundle3.putFloatArray("values", values3);
        intent3.putExtra("bundle",bundle3);
        

例子2:

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

public class TabTest extends TabActivity{
        private RadioGroup group;
        private TabHost tabHost;
        public static final String TAB_HOME="tabHome";
        public static final String TAB_MES="tabMes";
        public static final String TAB_TOUCH="tab_touch";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.maintabs);
                group = (RadioGroup)findViewById(R.id.main_radio);
                tabHost = getTabHost();
                tabHost.addTab(tabHost.newTabSpec(TAB_HOME)
                        .setIndicator(TAB_HOME)
                        .setContent(new Intent(this,Main.class)));
            tabHost.addTab(tabHost.newTabSpec(TAB_MES)
                        .setIndicator(TAB_MES)
                        .setContent(new Intent(this,Main2.class)));
            tabHost.addTab(tabHost.newTabSpec(TAB_TOUCH)
                            .setIndicator(TAB_TOUCH)
                            .setContent(new Intent(this,TouchTest.class)));
            group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                                switch (checkedId) {
                                case R.id.radio_button0:
                                        tabHost.setCurrentTabByTag(TAB_HOME);
                                        break;
                                case R.id.radio_button1:
                                        tabHost.setCurrentTabByTag(TAB_MES);
                                        break;
                                case R.id.radio_button2:
                                        tabHost.setCurrentTabByTag(TAB_TOUCH);
                                        break;

                                default:
                                        break;
                                }
                        }
                });
        }
}

效果如如下:
首先解决tab_host 的actitvty的跳转刷新,
  public void onCheckedChanged()方法进行group监控点击不同的事件响应,但是也只有点击不同的事件才会响应,这样问题就来了:比如同一个 actitvty进行 页面的缩放的的按钮就没有办法响应了。这里我是进行group立面的每一个RadioButton进行事件的处理 RadioButton.setOnClickListener().有人会说,不同的页面一旦显示一次当再次显示就不在刷新了,那么你可以这样设置一下 页面的跳转:

tabHost.addTab(tabHost
                                        .newTabSpec(TAB_NEXT)
                                        .setIndicator(TAB_NEXT)
                                        .setContent(
                                                        new Intent(new Intent(this, DrawReportActivity.class))
                                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
));

看看和上面代码有何不同,不错就是这里:        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)//就是这里起作用
下面解决进行页面传值的问题:
首先进行tab_host 向各个页面的传值:这个和普通的传值一样没有区别,

Intent intent_main = new Intent(this, DrawReportActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putInt("page", 1);
                        intent_main.putExtras(bundle);
                        tabHost.addTab(tabHost
                                        .newTabSpec(TAB_LAST)
                                        .setIndicator(TAB_LAST)
                                        .setContent(
                                                        new Intent(intent_main)
                                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

在相应的DrawReportActivity获取这个数据就可以
其次进行不同actitvty 之间传值的说明:
比如在A.actitvty要求跳转到B.actitvty里面,这里这样进行的跳转:
TabTest.tabHost.setCurrentTabByTag(TabTest.TAB_LAST);
将上面代码里面的tabHost进行静态化,进行group的跳转过去,这样就显示需要跳转的页面了,传值呢?传值在这里可以采取进行广播的方法:

发送广播:

Intent it = new Intent(action1);
it.putExtra("url", et.getText().toString());
sendBroadcast(it);

在注册Androidmanifest.xml进行声明:

<receiver android:name="com.raq.tab.Broadcastreceiver">
                <intent-filter>
                <action android:name="Broadcast_page_num" />
                </intent-filter>
</receiver>

得到相应的广播:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class Broadcastreceiver extends BroadcastReceiver {
        public String url;
        public void onReceive(Context context, Intent intent) {
                url = intent.getExtras().getString("url");
        }
}

进行传值,我觉得如果数据不是很多的话,完全可以写个静态类,进行存放一些数据,
这样跳转actitvty类得到时候进行同时的数据存放就可以了。

 

posted @ 2015-01-16 09:13  星辰之力  阅读(976)  评论(0编辑  收藏  举报