不同选项卡内的按钮
我有三个选项卡,每个选项卡都扩展 Activity 并实现 View.OnClickListener 当我选择某个选项卡并单击按钮时,在 SomeTabActivity 中实现的 OnClickListener 不起作用。为什么? 我需要做什么来创建我的活动? 每次我选择某个选项卡时,我都需要开始新的活动吗?
我的代码:
//主要活动
public class TripoidActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final Resources res = getResources();
createTabA(tabHost, res);
createTabB(tabHost, res);
createTabC(tabHost, res);
}
}
//选项卡A活动
public class TabAActivity extends Activity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
buttonAddItem = (Button) findViewById(R.id.btAddItem);
buttonAddItem.setOnClickListener(TabAActivity.this);
}
@Override
public void onClick(View v) {
//do something (show a toast msg)
}
}
//主要布局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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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" >
<include layout="@layout/tab_a" />
<include layout="@layout/tab_b" />
<include layout="@layout/tab_c" />
</FrameLayout>
</LinearLayout>
</TabHost>
I have three tabs and each one extends Activity and implements View.OnClickListener
When I selected some tab and click on a button, the OnClickListener implemented into SomeTabActivity doesn't work. Why?
What I need to do to create my activity?
Every time that I select some tab I need start new activity?
My code:
//the main activity
public class TripoidActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init() {
final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final Resources res = getResources();
createTabA(tabHost, res);
createTabB(tabHost, res);
createTabC(tabHost, res);
}
}
//the tab A activity
public class TabAActivity extends Activity implements View.OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
buttonAddItem = (Button) findViewById(R.id.btAddItem);
buttonAddItem.setOnClickListener(TabAActivity.this);
}
@Override
public void onClick(View v) {
//do something (show a toast msg)
}
}
//the main layout 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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
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" >
<include layout="@layout/tab_a" />
<include layout="@layout/tab_b" />
<include layout="@layout/tab_c" />
</FrameLayout>
</LinearLayout>
</TabHost>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个...
主要活动
选项卡活动
Try This...
Main Activity
Tab Activity