TabHost各个选项卡之间传递对象、参数及TabHost的使用

前端之家收集整理的这篇文章主要介绍了TabHost各个选项卡之间传递对象、参数及TabHost的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在叙述TabHost的使用之前,我们必须先知道TabActivity的生命周期,以了解主Activity中各个对象的实例化机制。

TabActivity中,只在第一次进入时走了onCreate()、onStart()、onResume()三个阶段,然后在退出页面时走了onPause()、onStop()和onDestroy()两个阶段。其他时间无论其中的子Activity如何切换,都不会再进入TabActivity的生命周期。

而子Activity,再第一次创建的时候,都会走onCreate()、onStart()、onResume()三个阶段,期间在各子Activity中切换,经历了onPause()和onResume()两个阶段,然后在主TabActivity退出时经历onPause()、onStop()和onDestroy()三个阶段。大致了解下TabActivity生命周期,再次不做细述。

//需要被传递的类

  1. package com.snail.intentobject;
  2.  
  3. import java.io.Serializable;
  4.  
  5. /**
  6. * PersonSer
  7. *
  8. * @author http://write.blog.csdn.net/postedit
  9. */
  10. public class PersonSer implements Serializable {
  11.  
  12. /**
  13. * serialVersionUID的作用是在修改实体类后,可以正常的序列化和反序列化,在此不多说,大家可以谷歌百度下。
  14. */
  15. private static final long serialVersionUID = -7620435178023928252L;
  16.  
  17. private String name;
  18.  
  19. private int age;
  20. public String getName() {
  21. return name;
  22. }
  23.  
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27.  
  28. public int getAge() {
  29. return age;
  30. }
  31.  
  32. public void setAge(int age) {
  33. this.age = age;
  34. }
  35.  
  36. }
  1. //TabActivity package com.example.lyktimi;
  2.  
  3. import com.example.music.musicList;
  4. import com.example.music.musicNameList;
  5. import com.example.music.myMusicList;
  6. import com.example.music.netMusicList;
  7. import com.example.server.musicServer;
  8.  
  9. import android.app.TabActivity;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12. import android.view.Menu;
  13. import android.view.View;
  14. import android.view.Window;
  15. import android.widget.RadioButton;
  16. import android.widget.TabHost;
  17.  
  18. public class MainActivity extends TabActivity {
  19. private TabHost tabHost;
  20. private RadioButton radioButton1;
  21. private RadioButton radioButton2;
  22. private PersonSer personser;//定义传递的对象
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. requestWindowFeature(Window.FEATURE_NO_TITLE);
  26. setContentView(R.layout.main);
  27. this.tabHost = getTabHost();
  28. radioButton1 = (RadioButton)findViewById(R.id.rb_tab1);
  29. radioButton2 = (RadioButton)findViewById(R.id.rb_tab2);
  30. radioButton3 = (RadioButton)findViewById(R.id.rb_tab3);
  31. radioButton4 = (RadioButton)findViewById(R.id.rb_tab4);
  32. personSer = new Personser(); personSer.setName("Name1");
  33. Intent bdMusicIntent = new Intent(getApplicationContext(),tab1.class);//激活Activity
  34. // this.tabHost.addTab(this.tabHost.newTabSpec("选项卡1")
  35. .setIndicator("选项卡1")
  36. .setContent(bdMusicIntent));
  37. Intent netMusicIntent = new Intent(getApplicationContext(),tab2.class);//激活Activity
  38. // netMusicIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  39. Bundle netbundle = new Bundle();
  40. netbundle.putString("parameter","parameter");//传递字符串参数
  41. netbundle.putParcelable("personSer",personSer);//传递对象
  42. netMusicIntent.putExtras(netbundle); this.tabHost.addTab(this.tabHost.newTabSpec("选项卡2") .setIndicator("选项卡2")
  43. .setContent(netMusicIntent));
  44. radioButton1.setOnClickListener(new mainRadio());
  45. radioButton2.setOnClickListener(new mainRadio());
  46. }
  47. class mainRadio implements android.view.View.OnClickListener{
  48.  
  49. public void onClick(View v) {
  50. switch (v.getId()) {
  51. case R.id.rb_tab1:
  52. tabHost.setCurrentTab(0);
  53. break;
  54. case R.id.rb_tab2:
  55. tabHost.setCurrentTab(1);
  56. break;
  57. }
  58.  
  59. }
  60. }
  61. public boolean onCreateOptionsMenu(Menu menu) {
  62. // Inflate the menu; this adds items to the action bar if it is present.
  63. getMenuInflater().inflate(R.menu.main,menu);
  64. return true;
  65. }
  66. //菜单项事件,每次点击菜单条目时调用
  67. // public boolean onOptionsItemSelected(MenuItem item) {
  68. //
  69. // switch (item.getItemId()) {
  70. // case R.id.findMusic:
  71. // Toast.makeText(getApplicationContext(),"正在查找",Toast.LENGTH_LONG).show();
  72. // getFile(path);
  73. // listInit();
  74. // break;
  75. //
  76. // default:
  77. // break;
  78. // }
  79. // return true;
  80. // }
  81.  
  82. }
  1. <pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@android:id/tabhost"
  6. >
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical"
  11. >
  12. <!-- 这个FrameLayout为TabWidget的内容 id必须写成@android:id/tabcontent的形式 -->
  13.  
  14. <FrameLayout
  15. android:id="@android:id/tabcontent"
  16. android:layout_width="fill_parent"
  17. android:layout_height="0.0dip"
  18. android:layout_weight="1.0"
  19. >
  20. </FrameLayout>
  21. <!-- TabWidget这个组件的ID 必须是@android:id/tabs -->
  22.  
  23. <TabWidget
  24. android:id="@android:id/tabs"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_weight="0.0"
  28. android:visibility="gone"
  29. >
  30. </TabWidget>
  31. <!-- android:background="#5f5f5f" -->
  32. <RadioGroup
  33. android:id="@+id/main_radioGroup"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="bottom"
  37. android:gravity="center_vertical"
  38. android:orientation="horizontal"
  39. >
  40. <RadioButton
  41. style="@style/tab_bottom_style"
  42. android:id="@+id/rb_tab1"
  43. android:checked="true"
  44. android:text="tab1"
  45. android:textSize="12sp"
  46. android:textColor="#ffffff"
  47. android:drawableTop="@drawable/tab_musiclist_selector"
  48. android:button="@null"
  49. />
  50. <RadioButton
  51. style="@style/tab_bottom_style"
  52. android:id="@+id/rb_tab2"
  53. android:text="tab2"
  54. android:textSize="12sp"
  55. android:textColor="#ffffff"
  56. android:drawableTop="@drawable/tab_mymusiclist_selector"
  57. android:button="@null"
  58. />
  59. </RadioGroup>
  60. </LinearLayout>
  61.  
  62. </TabHost>
  1.  
  1. //drawable文件夹下tab_mymusiclist_selector.xml文件 <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.  
  4. <item
  5. android:drawable="@drawable/tab_net_press"
  6. android:state_focused="true"
  7. android:state_pressed="false"
  8. />
  9. <item
  10. android:drawable="@drawable/tab_net_nor"
  11. android:state_enabled="true"
  12. android:state_pressed="true"
  13. />
  14. <item
  15. android:drawable="@drawable/tab_net_nor"
  16. android:state_checked="true"
  17. android:state_enabled="true"
  18. />
  19. <item
  20. android:drawable="@drawable/tab_net_press"
  21. />
  22.  
  23. </selector>

//选项卡2Activity(tab2)

  1. package com.example.music;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import com.example.lyktimi.R;
  9. import com.example.server.musicServer;
  10. import com.example.server.sharedPreferences;
  11.  
  12. import android.app.Activity;
  13. import android.content.Intent;
  14. import android.os.Bundle;
  15. import android.view.View;
  16. import android.widget.AdapterView;
  17. import android.widget.AdapterView.OnItemClickListener;
  18. import android.widget.ListView;
  19. import android.widget.SimpleAdapter;
  20.  
  21. public class netMusicList extends Activity {
  22. private TextView text;
  23. private PersonSer person;
  24. protected void onCreate(Bundle savedInstanceState) {
  25. // TODO Auto-generated method stub
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.net_music_activity);
  28.  
  29. }
  30. <pre class="java" name="code">/**
  31. * 得到MainActivity传来的对象实例
  32. */
  33. Intent intent = getIntent();
  34. Bundle bundle = intent.getExtra();
  35. person = (PersonSer)intent.getSerializableExtra("personSer");
  36. String str = bundle.getString("parameter");//取数据
  37. Log.i("TAG",str);
  38. person = intent.getSerializableExtra("personSer");//获得对象
  39. Log.i("TAG",person.getName);//显示对象内容

猜你在找的XML相关文章