我有三个选项卡,每个选项卡都扩展了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>
答案 0 :(得分:1)
试试这个...
主要活动
public class TabWidgetExampleActivity 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
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, DailyActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Daily").setIndicator("",
res.getDrawable(R.drawable.tab_daily))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, MapActivity.class);
spec = tabHost.newTabSpec("Map").setIndicator("",
res.getDrawable(R.drawable.tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, HourlyActivity.class);
spec = tabHost.newTabSpec("Hourly").setIndicator("",
res.getDrawable(R.drawable.tab_hour))
.setContent(intent);
tabHost.addTab(spec);
}
}
标签活动
public class HourlyActivity extends Activity{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Button btn=new Button(this);
btn.setText("button");
final LinearLayout l1=new LinearLayout(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "hai", Toast.LENGTH_LONG).show();
Intent i=new Intent();
i.setClass(getApplicationContext(), MapActivity.class);
startActivity(i);
}
});
setContentView(btn);
}
}