Android之Tab分页标签的实现方法一-----TabActivity和TabHost的结合(三)

  讲了两种方式,你是否发觉它们的代码耦合性太高,如果代码过多,那就是密密麻麻的一大堆,不仅可读性差,修改维护还很困难。这里讲到的方式三,能够很好的解决这个紧耦合问题。因为它的布局文件和各块代码都是独立的文件。那步入主题吧。

          三、TabActivity和TabHost的结合(三)

          实现描述:

          1.主类继承TabActivity

           public class Tagpage extends TabActivity

           2.获取当前TabHost对象

           final TabHost tabHost = getTabHost();

           3.添加Tab分页标签,这里就是关键,把每个分页面链接成Activity。页面的跳转,即是Activity的跳转。

           tabHost.addTab(tabHost.newTabSpec("Tab1")      
                .setIndicator("tab2", getResources().getDrawable(R.drawable.a1))      
                .setContent(new Intent(this, Page1.class)));

            .............

        

             

 

             

           1、布局文件

            page1.xml

           

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="This is Tab1"  
  11.     />  
  12. </LinearLayout>  

           page3.xml

           

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="This is Tab3"  
  11.     />  
  12. </LinearLayout>  

            page2.xml

           

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. android:id="@+id/widget30"  
  4. android:layout_width="fill_parent"  
  5. android:layout_height="fill_parent"  
  6. xmlns:android="http://schemas.android.com/apk/res/android"  
  7. android:orientation="vertical"  
  8. >  
  9. <EditText  
  10. android:id="@+id/et_text"  
  11. android:layout_width="fill_parent"  
  12. android:layout_height="wrap_content"  
  13. android:text="EditText"  
  14. android:textSize="18sp"  
  15. >  
  16. </EditText>  
  17. <Button  
  18. android:id="@+id/bt_show"  
  19. android:layout_width="149px"  
  20. android:layout_height="wrap_content"  
  21. android:text="显示"  
  22. >  
  23. </Button>  
  24. </LinearLayout>  

            2、代码

            主代码:Tagpage.java

            

[c-sharp]  view plain copy
  1. package com.myandroid.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.TabActivity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.widget.TabHost;  
  8.   
  9. public class Tagpage extends TabActivity{  
  10.     /** Called when the activity is first created. */  
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         //setContentView(R.layout.main);   
  15.         final TabHost tabHost = getTabHost();          
  16.         tabHost.addTab(tabHost.newTabSpec("Tab1")        
  17.                 .setIndicator("tab2", getResources().getDrawable(R.drawable.a1))        
  18.                 .setContent(new Intent(this, Page1.class)));        
  19.         tabHost.addTab(tabHost.newTabSpec("Tab2")        
  20.                 .setIndicator("tab2", getResources().getDrawable(R.drawable.a2))        
  21.                 .setContent(new Intent(this, Page2.class)));    
  22.                 // .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));  //添加这句话,会使得每次跳转到该页面都是新建一个页面,以往的数据状态会丢失,读者自己可以试验下  
  23.         tabHost.addTab(tabHost.newTabSpec("Tab3")        
  24.                 .setIndicator("tab2", getResources().getDrawable(R.drawable.a3))        
  25.                 .setContent(new Intent(this, Page3.class)));      
  26.                               
  27.     }  
  28. }  

            分页一Activity:Page1.java

           

[java]  view plain copy
  1. package com.myandroid.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Page1 extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.page1);  
  12.     }  
  13. }  

            分页三Activity:Page3.java

           

[java]  view plain copy
  1. package com.myandroid.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class Page3 extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.page3);  
  12.     }  
  13. }  

            分页二Activity:Page2.java

            

[java]  view plain copy
  1. package com.myandroid.test;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.Toast;  
  11.   
  12. public class Page2 extends Activity {  
  13.     private Button bt_show;  
  14.     private EditText et_text;  
  15.       
  16.     /** Called when the activity is first created. */  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.page2);  
  21.           
  22.         bt_show = (Button)findViewById(R.id.bt_show);  
  23.         et_text = (EditText)findViewById(R.id.et_text);  
  24.         bt_show.setOnClickListener(new ClickEvent());  
  25.     }  
  26.       
  27.     class ClickEvent implements OnClickListener{  
  28.   
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             // TODO Auto-generated method stub  
  32.             Toast.makeText(Page2.this, et_text.getText(), Toast.LENGTH_SHORT).show();  
  33.         }  
  34.           
  35.     }  
  36. }  

            最后,别忘了在AndroidManifest.xml文件注册上面用到三个子页面Activity,如下:

            

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.myandroid.test"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7. ..................  
  8.         <!-- 要添加Activity的声明,否则系统找不到响应的Activity,会报错 -->  
  9.         <activity android:name="Page1"></activity>  
  10.         <activity android:name="Page2"></activity>  
  11.         <activity android:name="Page3"></activity>  
  12.     </application>  
  13. </manifest>   
 

             介绍完了TabActivity和TabHost的结合分页,下一篇将介绍ActivityGroup + GridView结合实现Tap分页。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值