Buttons inside TAB not clickable/responding

被刻印的时光 ゝ 提交于 2020-01-06 08:41:41

问题


I have done TABS in my app using TabHost. one of the tabs has two buttons in it("button","anotherButton")

After I run my app I can see both tabs, but when I press on them nothing happens(they aren't even "clickable" when I press them.) And moreover, they don't respond to any listeners which I add to them.

Some code:

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+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">
        <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">
            <AnalogClock android:id="@+id/tab1" android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:layout_centerHorizontal="true" />

            <LinearLayout android:id="@+id/linearbuttonsview"
                android:orientation="vertical" android:layout_width="fill_parent"
                android:layout_height="fill_parent">


----pay attention here I decalred tab with two buttons !!! -----------------


                <Button android:id="@+id/button" android:layout_width="wrap_content"
                    android:onClick="onSemiButtonClick" android:layout_height="wrap_content" android:text="A semi-random button" />

                <Button android:id="@+id/anotherButton" android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:text="A semi-semi-random button"
                    android:scrollY="400px" />


            </LinearLayout>


            <ListView android:id="@+id/tablist" android:layout_width="fill_parent"

                android:layout_height="fill_parent" />




        </FrameLayout>
    </LinearLayout>
</TabHost>

This is my code:

public class TabDemo extends Activity
{
    private ListView ls1;
    private Button semiButton;

    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ls1 = new ListView(TabDemo.this);
        TabHost tabs = (TabHost) findViewById(R.id.tabhost);
        semiButton = (Button) findViewById(R.id.button);
        semiButton.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {

                int z = 0;
                z++;
            }

        });

        tabs.setup();

        // tab1
        TabSpec spec = tabs.newTabSpec("tag1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Clock");
        tabs.addTab(spec);

        // tab2
        spec = tabs.newTabSpec("tag2");
        spec.setContent(R.id.linearbuttonsview);
        spec.setIndicator("Button");
        tabs.addTab(spec);

        // tab3
        spec = tabs.newTabSpec("tag3");
        spec.setContent(new TabHost.TabContentFactory()
        {

            // we create a list view and give it as a reference to the the
            // content
            public View createTabContent(String tag)

            {

                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        TabDemo.this, android.R.layout.simple_list_item_1,
                        new String[]
                        { "item1", "item2", "item3" });

                ls1.setAdapter(adapter);

                return ls1;

            }

        });
        spec.setIndicator("List");
        tabs.addTab(spec);
    }
}

回答1:


some change in your code (create each tab with different activity)

public class TabBarExample extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
        TabSpec secondTabSpec = tabHost.newTabSpec("tid1");

        firstTabSpec.setIndicator("First Tab").setContent(new Intent(this,FirstTab.class));
        secondTabSpec.setIndicator("Second Tab").setContent(new Intent(this,SecondTab.class));

        tabHost.addTab(firstTabSpec);
        tabHost.addTab(secondTabSpec);

    }
}

FirstTab.java

public class FirstTab extends Activity {

    Button bt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs1);

        bt = (Button) findViewById(R.id.button1);

        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(FirstTab.this, "it's works", 2000).show();
            }
        });
    }
}

tab.xml

<TabHost android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:orientation="vertical" android:layout_height="fill_parent"
        android:layout_width="fill_parent">
        <TabWidget android:id="@android:id/tabs"
            android:layout_height="wrap_content" android:layout_width="fill_parent"></TabWidget>
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent"></FrameLayout>
    </LinearLayout>

</TabHost>

tab1.xml

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button android:text="Button" android:id="@+id/button1"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>


来源:https://stackoverflow.com/questions/7106733/buttons-inside-tab-not-clickable-responding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!