Android TabActivity使用

来源:互联网 发布:网络协议的作用和功能 编辑:程序博客网 时间:2024/05/29 18:56

        TabActivity里面使用的布局必须是TabHost而且TabHost布局文件里面应该有一个id为android:id="@android:id/tabs"的TabWidget。还应该有一个id为android:id="@android:id/tabcontent"的FragmenyLayout

         以上相当于是TabActivity的一个框架,然后我们怎么在里面增加一个Tab呢?

        通过gaiTabHost()来获取TabHost对象,然后addSpec()来增加tab,代码如下:

        

package com.example.hindintent;import java.net.MalformedURLException;import java.util.regex.Matcher;import java.util.regex.Pattern;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.widget.TabHost;import android.widget.Toast;public class MyTabActivity extends TabActivity {private String code_one;private String code_two;public void onCreate(Bundle saved){super.onCreate(saved);this.setContentView(R.layout.tabactivity1);TabHost tabHost = this.getTabHost();try {ReadCode rc = new ReadCode("http://www.nwpu.edu.cn/info/1003/12015.htm");while(true){if(rc.getCode()!="网页获取失败...请检查网络连接")break;}//获得的所有源码String code = rc.getCode();//Pattern p = Pattern.compile("<div id=\"framefUtIP8_left\"[\\s\\S]*?<div id=\"portal_block_1338_content\" class=\"dxb_bc\">");Pattern p_one = Pattern.compile("<table border=\"0\" class=\"winstyle58490\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" style=\"WORD-WRAP: break-word\">[\\s\\S]*?<\\/table>");Matcher m_one = p_one.matcher(code);Pattern p_two = Pattern.compile("<table width=\"100%\" cellspacing=\"5\" cellpadding=\"0\">[\\s\\S]*?</table>");Matcher m_two = p_two.matcher(code);if(m_one.find() && m_two.find()){String code_one = m_one.group();this.code_one = code_one;this.code_two = m_two.group();Log.e("preg", "---true----");}else{Log.e("preg", "  --false---");}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}Intent intentTwo = new Intent();Bundle argument1 = new Bundle();argument1.putString("code", this.code_two);intentTwo.putExtras(argument1);intentTwo.setAction("TabHostTwo");Intent intentOne = new Intent();Bundle argument = new Bundle();Log.e("click_pre","" +this.code_one);argument.putString("code", this.code_one);intentOne.putExtras(argument);intentOne.setAction("TabHostOne");tabHost.addTab(tabHost.newTabSpec("A").setIndicator("已接电话",this.getResources().getDrawable(R.drawable.ic_launcher)).setContent(intentOne));tabHost.addTab(tabHost.newTabSpec("B").setIndicator("未接电话",this.getResources().getDrawable(R.drawable.ic_launcher)).setContent(intentTwo));}}
    关于以上代码的解释(关于TabActivity):

            tabHost.newTabSpec("String")方法是给这个Tab加上一个Tag标签这个标签的内容有我们自己定

            setIndicator()方法相当于设计一个标题,就是这个Tab所对应的UI显示

            setContent(Intent)方法就是给这个TAB绑定一个点击启动的Intent

   关于TabActivity的未来

           网上看到有人说,TabActivity是个怪胎,他不符合Android单一界面的设计准则,所以即将会被Fragment代替,但我不这样认为,我觉得多窗口应该是未来的一个趋势,android应该改变单一窗口模式,想多窗口前进!

        

0 0
原创粉丝点击