您的位置:首页 > 移动开发 > Android开发

android_TabHost设置点击事件newTapSpec_141001

2014-09-30 09:57 113 查看
找对TabHost的tab设置点击事件,然后,在网上发现了好多的错误或者过时的代码,这代码害人啊~






大致总结一下,个人总结的简单的设置点击事件的方法

两种方法:

1)第一种方法,继承于TabActivity (这种方法已经弃用,不过,为了记住查询时,他人的错误做法贴上代码)

2)第二种方法,继承于Activity, 这是目前使用的方法,主要为使用newTabSpec建立tabID设置点击事件

第一种方法(TabActivity) : [代码]

//MainActivity.java文件

package com.yline.tabhost_tabac;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.Toast;

public class MainActivity extends TabActivity implements OnTabChangeListener {
	private TabHost tabHost;

	@Override  
	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  

		tabHost = this.getTabHost();

		LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView());  

		tabHost.addTab(tabHost.newTabSpec("tabOne")    
				.setIndicator("界面1", getResources().getDrawable(R.drawable.ic_launcher))    
				.setContent(R.id.tab1));  

		tabHost.addTab(tabHost.newTabSpec("tabTwo")    
				.setIndicator("界面2")    
				.setContent(R.id.tab2));

		tabHost.addTab(tabHost.newTabSpec("tabThree")    
				.setIndicator("界面3")    
				.setContent(R.id.tab3));

		tabHost.setOnTabChangedListener(this);         
	}

	public void onTabChanged(String tabId){  
		if(tabId.equals("tabOne")){
			Toast.makeText(this, "分页1", 0).show();  
		}  
		if(tabId.equals("tabTwo")){  
			Toast.makeText(this, "分页2", 0).show();  
		}  
		if(tabId.equals("tabThree")){  
			Toast.makeText(this, "分页3", 0).show();  
		}  
	}  
}
// activity_main.xml文件

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_111" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_222" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_333" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>
第一种方法 --> 运行结果:

操作:

1)点击本tab时,没有反应

2)点击其他tab时,会跳出一个 toast..maketext; 提示当前分页



第二种方法(Activity) : [代码]

//MainActivity.java 文件

package com.yline.tabhost_ac;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends Activity implements OnTabChangeListener {
	private TabHost tabHost;
	private String tabA = "TAB_A";
	private String tabB = "TAB_B";
	private String tabC = "TAB_C";
	private Intent intent_A;
	private Intent intent_B;
	private Intent intent_C;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup();

		intent_A = new Intent(MainActivity.this, TAB_A.class);
		intent_B = new Intent(MainActivity.this, TAB_B.class);
		intent_C = new Intent(MainActivity.this, TAB_C.class);

		tabHost.addTab(tabHost.newTabSpec(tabA)
				.setIndicator("界面A")
				.setContent(R.id.tab1));

		tabHost.addTab(tabHost.newTabSpec(tabB)
				.setIndicator("界面B")
				.setContent(R.id.tab2));

		tabHost.addTab(tabHost.newTabSpec(tabC)
				.setIndicator("界面C")
				.setContent(R.id.tab3));

		tabHost.setCurrentTab(0);   //防止重复点击 产生错误

		tabHost.setOnTabChangedListener(this);
	}

	@Override
	public void onTabChanged(String tabId) {
		if (tabId.equals(tabA)) {
			startActivity(intent_A);
		} else if (tabId.equals(tabB)) {
			startActivity(intent_B);
		} else if (tabId.equals(tabC)) {
			startActivity(intent_C);
		}

	}
}
注释:

tabHost.setCurrentTab(0) ; 一定要写, 这是防止重复点击tabid,而跳出对应的layout

//activity_main.xml文件

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_111" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_222" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="tabhost_button_333" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>
// AndroidMainfest.xml 文件

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.yline.tabhost_ac.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.yline.tabhost_ac.TAB_A">
        </activity>
        <activity 
            android:name="com.yline.tabhost_ac.TAB_B">
        </activity>
        <activity 
            android:name="com.yline.tabhost_ac.TAB_C">
        </activity>
    </application>


其他TAB_A.java, TAB_B.java, TAB_C.java ; 以及 tab_a.xml , tab_b.xml , tab_c.xml 都是可以自定义的

我贴上其中一组 TAB_A.java 以及 tab_a.xml

// TAB_A.java文件

package com.yline.tabhost_ac;

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

public class TAB_A extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_a);
	}
	
}
//tab_a.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tab_a" />

</LinearLayout>


第二种方法 --> 运行结果:

操作:

1)点击本tab时,没有反应

2)点击其他tab时,会跳出一个 新的Activity (第二幅图)





本事例 源代码 下载 (可运行):
http://pan.baidu.com/s/1c0gY4Ac

个人参考文件:

http://www.cnblogs.com/tt_mc/archive/2010/05/26/1744502.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: