TAB中的按钮不可点击/响应 [英] Buttons inside TAB not clickable/responding
本文介绍了TAB中的按钮不可点击/响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经使用TabHost在我的应用程序中完成了TABS。
其中一个标签有两个按钮( button, anotherButton)
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.
某些代码:
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>
这是我的代码:
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);
}
}
推荐答案
一些更改代码(创建具有不同活动的每个选项卡)
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
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>
这篇关于TAB中的按钮不可点击/响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文