GVKun编程网logo

Android入门之TabHost与TabWidget实例解析(android tab)

19

本文的目的是介绍Android入门之TabHost与TabWidget实例解析的详细情况,特别关注androidtab的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解

本文的目的是介绍Android入门之TabHost与TabWidget实例解析的详细情况,特别关注android tab的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解Android入门之TabHost与TabWidget实例解析的机会,同时也不会遗漏关于"tabhost requires a tabwidget with id..." 问题解决、Android TabHost.addTab – >空指针异常、Android TabWidget、Android TabWidget切换卡的实现应用的知识。

本文目录一览:

Android入门之TabHost与TabWidget实例解析(android tab)

Android入门之TabHost与TabWidget实例解析(android tab)

本文实例介绍的是Android的Tab控件,Tab控件可以达到分页的效果,让一个屏幕的内容尽量丰富,当然也会增加开发的复杂程度,在有必要的时候再使用。Android的Tab控件使用起来有点奇怪,必须包含和按照以下的顺序:

TabHost控件->TabWidget(必须命名为tabs)->FrameLayout(必须命名为tabcontent)。

先来贴出本例运行的截图:

main.xml的源码如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
  android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/TabHost1">
  <TabWidget android:id="@android:id/tabs"
    android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
  <FrameLayout android:id="@android:id/tabcontent"
    android:paddingTop="65px" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab1" android:orientation="vertical" android:layout_width="fill_parent">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab1" android:layout_width="fill_parent"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab1" android:text="Tab1"></Button>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:id="@+id/Tab2" android:layout_width="fill_parent" android:orientation="horizontal">
      <EditText android:layout_height="wrap_content" android:id="@+id/edtTab2" android:layout_width="wrap_content" android:layout_weight="300"></EditText>
      <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnTab2" android:text="Tab2"></Button></LinearLayout>
  </FrameLayout>
</TabHost>

java程序源码如下:

package com.testTab;
import android.app.tabactivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class testTab extends tabactivity {//基于tabactivity构建
 
 Button btnTab1,btnTab2;
 EditText edtTab1,edtTab2;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    TabHost tabs = getTabHost();
    //设置Tab1
    TabSpec tab1 = tabs.newTabSpec("tab1");
    tab1.setIndicator("tab1");   // 设置tab1的名称
    tab1.setContent(R.id.Tab1);  // 关联控件
    tabs.addTab(tab1);        // 添加tab1
    
    btnTab1=(Button)this.findViewById(R.id.btnTab1);
    edtTab1=(EditText)this.findViewById(R.id.edtTab1);
    btnTab1.setonClickListener(new ClickEvent());
    
    //设置Tab2
    TabSpec tab2 = tabs.newTabSpec("tab2");
    tab2.setIndicator("tab2");   
    tab2.setContent(R.id.Tab2);  
    tabs.addTab(tab2);        
    
    btnTab2=(Button)this.findViewById(R.id.btnTab2);
    edtTab2=(EditText)this.findViewById(R.id.edtTab2);
    btnTab2.setonClickListener(new ClickEvent());
    
    tabs.setCurrentTab(0);
  }
  
  class ClickEvent implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  if(v==btnTab1)
  {
  edtTab1.setText("tab1");
  }
  else if(v==btnTab2)
  {
  edtTab2.setText("tab2");
  }
 }
  
  }
}

"tabhost requires a tabwidget with id..." 问题解决

当出现:tabhost requires a tabwidget with id 。。。。问题时,是因为 

android:id="@android:id/tabcontent"

这个写成了

android:id="@+id/tabcontent"

 

Android TabHost.addTab – >空指针异常

Android TabHost.addTab – >空指针异常

这是我的代码:
public class Main extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            TabHost tabHost = new TabHost(this);

            TabHost.TabSpec tab = tabHost.newTabSpec("tab1");
            tab.setIndicator("Tab 1");
            tab.setContent(new TabHost.TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                    TextView tv = new TextView(Main.this);
                    tv.setText("tab 1 content");
                    return tv;
                }
            });

            tabHost.addTab(tab);

            setContentView(tabHost);
        }
    }

我得到这个错误:

[...]
    07-13 20:26:49.261: ERROR/AndroidRuntime(625): Caused by: java.lang.NullPointerException
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at android.widget.TabHost.addTab(TabHost.java:206)
    07-13 20:26:49.261: ERROR/AndroidRuntime(625):     at test.test.Main.onCreate(Main.java:27)
    [...]

我需要通过代码来做,我不能使用XML.有人可以帮我修理这段代码吗?

解决方法

您应该使用tabactivity,它需要将相同的特殊布局设置为内容(请参阅 http://developer.android.com/resources/tutorials/views/hello-tabwidget.html).如果你不能使用xml,你应该从java代码构造相同的内容:
public class Main extends tabactivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = new TabHost(this);
    tabHost.setId(android.R.id.tabhost);

    TabWidget widget = new TabWidget(this);
    widget.setId(android.R.id.tabs);

    FrameLayout content = new FrameLayout(this);
    content.setId(android.R.id.tabcontent);

    LinearLayout layout = new LinearLayout(this);
    layout.setorientation(LinearLayout.VERTICAL);
    layout.addView(widget);
    layout.addView(content);

    tabHost.addView(layout);

    setContentView(tabHost);

    TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 1 content");
            return tv;
        }
    });

    tabHost.addTab(tab1);

    TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(new TabHost.TabContentFactory() {
        @Override
        public View createTabContent(String tag) {
            TextView tv = new TextView(Main.this);
            tv.setText("tab 2 content");
            return tv;
        }
    });

    tabHost.addTab(tab2);

    setContentView(tabHost);
}

}

Android TabWidget

Android TabWidget

先做个记录,回头分析。下面 xml 文件实现了 TabWidget 位于底部。

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android :id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <FrameLayout
            android:id="@android :id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

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

</TabHost>

 

Android TabWidget切换卡的实现应用

Android TabWidget切换卡的实现应用

TabWidget类似于Android 中查看电话薄的界面,通过多个标签切换显示不同内容。要实现这一效果,首先要了解TabHost,它是一个用来存放多个Tab标签的容器。每一个Tab都可以对应自己的布局,比如,电话薄中的Tab布局就是一个List的线性布局了。 
要使用TabHost,首先需要通过getTabHost方法来获取TabHost的对象,然后通过addTab方法来向TabHost中添加 Tab。当然每个Tab在切换时都会产生一个事件,要捕捉这个事件需要设置tabactivity的事件监听 setonTabChangedListener。

1、布局文件

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

  <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" />

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

      <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Linux"
        android:textColor="#FF0000" />

      <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="MAC"
        android:textColor="#385E0F" />

      <TextView
        android:id="@+id/textview3"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Windows"
        android:textColor="#1E90FF" />
    </FrameLayout>
  </LinearLayout>

</TabHost>

2、修改MainActivity,注意是继承自tabactivity

public class MainActivity extends tabactivity {

 private TabHost tabHost;

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

 tabHost = getTabHost();

 addTab();// 添加标签
 // 设置TabHost背景颜色
 tabHost.setBackgroundColor(Color.argb(150,20,80,150));
 // 设置TabHost背景图片资源
 tabHost.setBackgroundResource(R.drawable.ic_launcher);
 // 设置当前显示哪一个标签 我的理解就是当你第一次启动程序默认显示那个标签 这里是指定的选项卡的ID从0开始
 tabHost.setCurrentTab(0);
 // 标签切换事件处理,setonTabChangedListener 注意是标签切换事件不是点击事件,而是从一个标签切换到另外一个标签会触发的事件
 tabHost.setonTabChangedListener(new OnTabchangelistener() {
  @Override
  public void onTabChanged(String tabId) {
  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  Dialog dia;
  builder.setTitle("提示");
  builder.setMessage("当前选中了" + tabId + "标签");
  builder.setPositiveButton("确定",new OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog,int which) {
   dialog.cancel();
   }
  });
  dia = builder.create();
  dia.show();
  }
 });
 }

 // 为TabHost添加标签 新建一个newTabSped(new TabSpec) 设置其标签和图标(setIndicator)、设置内容(setContent)
 // TabSpec是TabHost的内部类 TabHost对象的 newTabSpec()方法返回一个TabSpec对象
 // 源码里边是这么写的 public TabSpec newTabSpec(String tag)
 // { return new TabSpec(tag); }
 private void addTab() {
 tabHost.addTab(tabHost
  .newTabSpec("tab1")
  .setIndicator("TAB1",getResources().getDrawable(R.drawable.ic_launcher))// setIndicator()此方法用来设置标签和图表
  .setContent(R.id.textview1));
 // 指定内容为一个TextView --->public TabHost.TabSpec setContent(int viewId) 此方法需要一个 viewId 作为参数
 tabHost.addTab(tabHost
  .newTabSpec("tab2")
  .setIndicator("TAB2",getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview2));

 tabHost.addTab(tabHost
  .newTabSpec("tab3")
  .setIndicator("TAB3",getResources().getDrawable(R.drawable.ic_launcher))
  .setContent(R.id.textview3));
 }
}

 3、运行程序:如下!

关于Android入门之TabHost与TabWidget实例解析android tab的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于"tabhost requires a tabwidget with id..." 问题解决、Android TabHost.addTab – >空指针异常、Android TabWidget、Android TabWidget切换卡的实现应用的相关信息,请在本站寻找。

本文标签: