开发者社区> 问答> 正文

使用TabHost和Button进行Android 1.5编程

我目前正在试用Android 1.5 SDK,并且在TabHost上看到了几个示例。我想做的是在每个选项卡上使用一个不同的按钮来执行其任务。

我尝试使用的是onClickListiner()和onClick()。我认为这是所有开发人员使用的方式,但是每次按下按钮时,LogCat上都会收到一个空异常。我也有每个XML布局,所以我将Tab称为:tab.add(... setContent(R.id.firstTabLayout))

firstTabLayout = layout for Button and TextView. 在TabHost下使按钮/ TextView正常工作的最佳方法是什么?

展开
收起
Puppet 2020-01-20 09:55:50 430 0
1 条回答
写回答
取消 提交回答
  • 我不确定您的问题出在哪里,但这是我之前设置Tabbed活动的方式

    
    layout.xml
    
    <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">
    
        <LinearLayout android:id="@+id/tab1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView android:id="@android:id/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
        <LinearLayout android:id="@+id/tab2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView android:id="@android:id/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
        <LinearLayout android:id="@+id/tab3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    
            <TextView android:id="@android:id/text"
                android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
    
        </LinearLayout>
    </FrameLayout>
    Tab.java
    
    public class InitialActivity extends TabActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
            TabHost th = getTabHost();
            th.addTab(th.newTabSpec("tab_1").setIndicator("Tab1").setContent(R.id.tab1));
            th.addTab(th.newTabSpec("tab_2").setIndicator("Tab2").setContent(R.id.tab2));
            th.addTab(th.newTabSpec("tab_3").setIndicator("Tab3").setContent(R.id.tab3));
        }
    }
    

    然后,您可以在选项卡中的任何视图上使用findViewById(),或者如果它们之间存在共享名称,则可以执行 findViewById(R.id.tab1).findViewById(R.id.text)

    2020-01-20 09:56:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载