2012-12-14 34 views
1

這是我的選項卡代碼。請告訴我如何通過switch語句或簡單的onclick監聽器將監聽器放在tab1上。而這個敬酒消息也沒有工作如何把聽衆放在android中的選項卡上

public class TabsActivity extends Activity implements OnClickListener { 

    TabHost th; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_tabs); 

    th = (TabHost)findViewById(R.id.tabhost); 

    th.setup(); 
    TabSpec specs = th.newTabSpec("tag1"); 
    specs.setContent(R.id.tab1); 
    specs.setIndicator("Home"); 
    th.addTab(specs); 


    specs = th.newTabSpec("tag2"); 
    specs.setContent(R.id.tab2); 
    specs.setIndicator("Goup"); 
    th.addTab(specs); 

    specs = th.newTabSpec("tag3"); 
    specs.setContent(R.id.tab3); 
    specs.setIndicator("Delete"); 
    th.addTab(specs); 

} 



@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch(arg0.getId()){ 
    case R.id.tab1: 
     Toast.makeText(TabsActivity.this, "Action Item 0 selected!", Toast.LENGTH_LONG).show(); 
    break; 
case R.id.tab2: 

    TextView text= new TextView(TabsActivity.this); 
    text.setText("u have created a new tab"); 

    break; 

case R.id.tab3: 
    break; 

} 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.activity_tabs, menu); 
return true; 
} 
} 

回答

3

如果你想監聽器操作點擊選項卡上,你應該這樣做:

th.setOnTabChangedListener(new OnTabChangeListener() { 
    @Override 
    public void onTabChanged(String tabId) { 
     Toast.makeText(getApplicationContext(), "Click on tab: "+ tabId, Toast.LENGTH_SHORT).show(); 
    } 
});