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生命周期,再次不做细述。

//需要被传递的类

package com.snail.intentobject;

import java.io.Serializable;

/**
 * PersonSer
 * 
 * @author http://write.blog.csdn.net/postedit
 */
public class PersonSer implements Serializable {

	/**
	 * serialVersionUID的作用是在修改实体类后,可以正常的序列化和反序列化,在此不多说,大家可以谷歌百度下。
	 */
	private static final long serialVersionUID = -7620435178023928252L;

	private String name;

	private int age;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}
//TabActivity																										        package com.example.lyktimi;

import com.example.music.musicList;
import com.example.music.musicNameList;
import com.example.music.myMusicList;
import com.example.music.netMusicList;
import com.example.server.musicServer;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.TabHost;

public class MainActivity extends TabActivity {
	
	private TabHost tabHost;
	private RadioButton radioButton1;
	private RadioButton radioButton2; 
		
	private PersonSer personser;//定义传递的对象
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		this.tabHost = getTabHost();
		radioButton1 = (RadioButton)findViewById(R.id.rb_tab1);
		radioButton2 = (RadioButton)findViewById(R.id.rb_tab2);
		radioButton3 = (RadioButton)findViewById(R.id.rb_tab3); 
		radioButton4 = (RadioButton)findViewById(R.id.rb_tab4); 
		
		personSer = new Personser();											personSer.setName("Name1");	
		Intent bdMusicIntent = new Intent(getApplicationContext(),tab1.class);//激活Activity
//		this.tabHost.addTab(this.tabHost.newTabSpec("选项卡1")
				.setIndicator("选项卡1")
				.setContent(bdMusicIntent));
				
		Intent netMusicIntent = new Intent(getApplicationContext(),tab2.class);//激活Activity
//		netMusicIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		Bundle netbundle = new Bundle();
		netbundle.putString("parameter","parameter");//传递字符串参数
		netbundle.putParcelable("personSer",personSer);//传递对象
		netMusicIntent.putExtras(netbundle); 										this.tabHost.addTab(this.tabHost.newTabSpec("选项卡2")                                     	                    .setIndicator("选项卡2")
				.setContent(netMusicIntent));
		
				
		radioButton1.setOnClickListener(new mainRadio());
		radioButton2.setOnClickListener(new mainRadio());
	}
	
	class mainRadio implements android.view.View.OnClickListener{

		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.rb_tab1:
				
				tabHost.setCurrentTab(0);
				
				break;
			case R.id.rb_tab2:
				
				tabHost.setCurrentTab(1);
				break;
			}

		}
		
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main,menu);
		return true;
	}
	//菜单项事件,每次点击菜单条目时调用
//	public boolean onOptionsItemSelected(MenuItem item) {
//		
//		switch (item.getItemId()) {
//		case R.id.findMusic:
//		        Toast.makeText(getApplicationContext(),"正在查找",Toast.LENGTH_LONG).show();
//			getFile(path);
//        	listInit();
//			break;
//
//		default:
//			break;
//		}
//		return true;
//	}

}
<pre class="java" name="code"><?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@android:id/tabhost"
    >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        >
         
        <!-- 这个FrameLayout为TabWidget的内容   id必须写成@android:id/tabcontent的形式 -->

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0" 
            >
        </FrameLayout>
        
        <!-- TabWidget这个组件的ID 必须是@android:id/tabs -->

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:visibility="gone" 
            >
        </TabWidget>
        
        <!-- android:background="#5f5f5f" -->
        <RadioGroup
            android:id="@+id/main_radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal" 
            >
            
            <RadioButton
                style="@style/tab_bottom_style"
                android:id="@+id/rb_tab1"
                android:checked="true"
                android:text="tab1"
                android:textSize="12sp"  
                android:textColor="#ffffff"
                android:drawableTop="@drawable/tab_musiclist_selector"
                android:button="@null"
                />
            <RadioButton
                style="@style/tab_bottom_style"
                android:id="@+id/rb_tab2"
                android:text="tab2"
                android:textSize="12sp" 
                android:textColor="#ffffff"
                android:drawableTop="@drawable/tab_mymusiclist_selector"
                android:button="@null"
                />
                   
        </RadioGroup>
        
    </LinearLayout>

</TabHost>											   
 
//drawable文件夹下tab_mymusiclist_selector.xml文件								              <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item 
        android:drawable="@drawable/tab_net_press" 
        android:state_focused="true" 
        android:state_pressed="false"
        />
    <item 
        android:drawable="@drawable/tab_net_nor" 
        android:state_enabled="true" 
        android:state_pressed="true"
        />
    <item 
        android:drawable="@drawable/tab_net_nor" 
        android:state_checked="true" 
        android:state_enabled="true"
        />
    <item 
        android:drawable="@drawable/tab_net_press"
        />

</selector>

//选项卡2Activity(tab2)

package com.example.music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.lyktimi.R;
import com.example.server.musicServer;
import com.example.server.sharedPreferences;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class netMusicList extends Activity {
	
	private TextView text;	
	private PersonSer person;	
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.net_music_activity);
				 

}
<pre class="java" name="code">/**
	 * 得到MainActivity传来的对象实例
	 */
	Intent intent = getIntent();
	Bundle bundle = intent.getExtra();											
	person = (PersonSer)intent.getSerializableExtra("personSer");
	String str = bundle.getString("parameter");//取数据									
	Log.i("TAG",str);													
	person = intent.getSerializableExtra("personSer");//获得对象	
	Log.i("TAG",person.getName);//显示对象内容
原文链接:https://www.f2er.com/xml/298070.html

猜你在找的XML相关文章