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

           

           page3.xml

           

            page2.xml

           

            2、代码

            主代码:Tagpage.java

            package com.myandroid.test; import android.app.Activity; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TabHost; public class Tagpage extends TabActivity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); final TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("Tab1") .setIndicator("tab2", getResources().getDrawable(R.drawable.a1)) .setContent(new Intent(this, Page1.class))); tabHost.addTab(tabHost.newTabSpec("Tab2") .setIndicator("tab2", getResources().getDrawable(R.drawable.a2)) .setContent(new Intent(this, Page2.class))); // .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); //添加这句话,会使得每次跳转到该页面都是新建一个页面,以往的数据状态会丢失,读者自己可以试验下 tabHost.addTab(tabHost.newTabSpec("Tab3") .setIndicator("tab2", getResources().getDrawable(R.drawable.a3)) .setContent(new Intent(this, Page3.class))); } }

            分页一Activity:Page1.java

            package com.myandroid.test; import android.app.Activity; import android.os.Bundle; public class Page1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page1); } }

            分页三Activity:Page3.java

            package com.myandroid.test; import android.app.Activity; import android.os.Bundle; public class Page3 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page3); } }

            分页二Activity:Page2.java

             package com.myandroid.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Page2 extends Activity { private Button bt_show; private EditText et_text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.page2); bt_show = (Button)findViewById(R.id.bt_show); et_text = (EditText)findViewById(R.id.et_text); bt_show.setOnClickListener(new ClickEvent()); } class ClickEvent implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(Page2.this, et_text.getText(), Toast.LENGTH_SHORT).show(); } } }

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

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

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

            

你可能感兴趣的:(Android入门)