java - Android 中的选项卡布局和按钮

标签 java android button

我使用选项卡布局在 android 中构建了一个应用程序,它有 2 个选项卡。我想在每个选项卡上都有一个不同的按钮。但如果我在 main.xml 中定义按钮,我会在两个选项卡上得到相同的按钮。

我什至尝试在每个选项卡的类中单独定义按钮,但遇到了一些奇怪的错误。有人可以帮我解决这个问题吗?下面是我的代码:

FinalProj.java:

package FinalProj.com;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class FinalProj extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        intent = new Intent().setClass(this, iFallApp.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                          res.getDrawable(R.drawable.icon))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Settings.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                          res.getDrawable(R.drawable.icon))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);
    }
}

对于 Tab1 - iFallApp.java

package FinalProj.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class iFallApp extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textview = new TextView(this);
        textview.setText("This is the iFall tab");
        setContentView(textview);
    }
}

选项卡 2 - Settings.java

package FinalProj.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Settings extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TextView textview = new TextView(this);
        textview.setText("This is the Settings tab");
        setContentView(textview);
    }
}

Main.xml:

<?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:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <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"
            android:padding="5dp" />
</LinearLayout>
</TabHost>

我想在 iFallApp.java 中拥有一个按钮和 TextView,在 Settings.java 中拥有一个按钮和 editText。

最佳答案

对于发现此问题的其他人:

public class FinalProj extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

setContentView(R.layout.main)告诉 Activity 在其 UI 上显示什么。在本例中, View 被设置为从对应于/layout/main.xml 的 R.layout.main 加载(R 是由 android 框架生成的生成的查找类)。

更改 main.xml 会更改两个选项卡,因为这两个选项卡都包含在 FinalProj 内。 TabHost内的 Activity View 定义在 main.xml 内部这里:

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

这里详细描述了整个选项卡系统: Tab Layout | Android Developers

关于java - Android 中的选项卡布局和按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918764/

相关文章:

javascript - 单击按钮导航至路线

java - 您的 Java 低延迟应用程序的开发 list 是什么?

数组的Java ArrayList?

java - 为什么选择巴克敏斯特而非Maven?

java - 读取文本文件并将其拆分为数组 - Android

java - 无法在 Android 中使用更新的 Dagger 2.11 API

android - WebView 摆脱双击缩放。

html - 在 Safari 和 Chrome 中提交图像边框?

java - Log4j 验证异常是否作为最后一个参数传递

html - UI 最佳实践问题 : Cancel button or Cancel link