1
我有4個選項卡,並在1選項卡(選項卡3)我有一個列表2選項。點擊其中一個選項時,是否有辦法開始新的活動?我嘗試將Activity更改爲ListActivity,但是一旦它加載,我的應用就會崩潰。我是否需要爲此實施其他一些措施?開始一個新的活動在列表中的一個選項卡
代碼:
public class MainActivity extends Activity implements OnClickListener{
TabHost th;
ListView libraryView;
String libraryList[] = {"Item1", "Item2"};
@Override
public void onCreate(Bundle savedInstanceState) {
//Always do this at the start.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_white_text, R.id.library_list_content, libraryList);
libraryView = (ListView) findViewById(R.id.listView1);
libraryView.setAdapter(adapter);
//Creating the TabHost and gathering its ID for usage.
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("tab1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("tab2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("tab3");
th.addTab(specs);
specs = th.newTabSpec("tag4");
specs.setContent(R.id.tab4);
specs.setIndicator("tab4");
th.addTab(specs);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.listView1:
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/black" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp" >
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>
列表視圖中添加的點擊監聽器列表視圖 –
嘿RajaReddy PI實現OnClickListener,並可能你舉了一個小例子,說明我該怎麼做,或者請參考如何去做關於這樣做。更新代碼。 – JoeyL
我添加了onClick(View v),這是否是向前邁進的一步,如果是這樣,是否是開關,情況R.id.listView1是否正確? – JoeyL