2012-02-22 65 views
0

我有三个选项卡,每个选项卡扩展活动并实现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

为什么buttonAddItem.setOnClickListener(TabAActivity.this); - 为什么不只是'这个'? – John3136 2012-02-22 00:53:57

+0

是一回事。但这种方式“TabAActivity.this”是最容易看到你所指的。 – 2012-02-22 01:23:38

+0

让我的大脑沿着“我不知道这是否与班上的某些事情做了某些事情,而不是我目前所在的班级的实例”这样的句子。如果你修改东西,也有可能会破坏多态性的风险。不过,我不认为这是问题:-( – John3136 2012-02-22 02:39:20

回答

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); 
} 

} 
+0

嗨,tks for help.I解决了我的问题,我只需要将“setContent(R.id.tabId)”更改为“setContent(someIntent)”。每次更改选项卡时,它都会启动一项新活动。 – 2012-02-22 21:39:38