android_TabHost设置点击事件newTapSpec_141001
来源:互联网 发布:ubuntu rar 解压命令 编辑:程序博客网 时间:2024/04/29 03:47
找对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;@Overridepublic 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);}@Overridepublic 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{@Overrideprotected 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
0 0
- android_TabHost设置点击事件newTapSpec_141001
- Android_TabHost
- Android_TabHost
- SecondActivity 设置点击事件
- EditText 设置点击事件
- 设置点击监听事件
- recyclerview点击事件设置
- 给LinearLayout设置点击事件
- RecyclerView设置Item点击事件
- RecyclerView设置 item点击事件
- 设置XML点击效果事件
- RecyclerView item 点击事件设置
- Android----------Recyclerview设置点击事件
- 设置 点击回车触发事件
- Android RecyclerView设置点击事件
- 关于VLayout设置点击事件
- Textview设置超链接点击事件
- GridView 设置setOnItemClickListener点击事件不响应
- 穿戴式智能设备有哪些潜在安全问题?(下)
- cocos2dx 实现果冻,刀光等效果
- Libimseti推荐系统
- Tcpdump抓包命令
- 我读经典(7):读《程序员生存定律》有感
- android_TabHost设置点击事件newTapSpec_141001
- 根据CentOS定制自己的发行版
- 浅谈smarty模板的mvc框架
- Linux 线程实现机制分析
- Leetcode--Maximum Subarray
- DirectX 9.0c游戏开发手记之RPG编程自学日志之17: Drawing with DirectX Graphics (用DirectX图形绘图)(第13节)
- css 属性规定要显示的光标的类型(形状)如手状
- openwrt系统 sysupgrade 命令执行过程分析
- java实现生成简单图片验证码