2012-02-22 14 views
0

3つのタブがあり、それぞれアクティビティを拡張して実装します。View.OnClickListener Someタブを選択してボタンをクリックすると、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); 

    } 

} 

//タブ活動

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

+0

同じことです。しかし、このように "TabAActivity.this"は、あなたが何を参照するのが最も簡単です。 –

+0

私の頭脳は、 "これは何とか私が現在いるクラスのインスタンスではなく、クラスで何かをするかどうか疑問に思っています。また、事柄を変更した場合、ポリモーフィズムを破る可能性があります。しかし、私はこれが問題だとは思っていません:-( – John3136

答えて

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。私の問題を解決しました。 "setContent(R.id.tabId)"を "setContent(someIntent)"に変更するだけでした。タブを変更するたびに新しいアクティビティが開始されます。 –

関連する問題