2012-04-20 41 views
1

我有以下TabActivityAndroid的:如何在TabActivity基於標籤選擇換的ImageButton

enter image description here

現在,當我選擇「結算額」選項卡,在頂部的ImageButton應更改爲另一個的ImageButton ,當選擇「成員」選項卡時,不應顯示任何按鈕。 ImageButton在TabActivity中。如何根據選擇標籤動態地實現按鈕更改?

這裏是我的TabActivity:

public class TransactionTab extends TabActivity{ 
    String groupid=""; 
    TabHost tb; 
    TabSpec tab1,tab2,tab3; 
    Intent translist,settleamt,mems; 
    Context context = TransactionTab.this; 
    ImageButton v_createtrans; 

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

     Intent grpintent = getIntent(); 
     groupid = grpintent.getStringExtra("grpid"); 

     tb = (TabHost)findViewById(android.R.id.tabhost); 
     v_createtrans = (ImageButton)findViewById(R.id.x_createtrans); 

     tab1 = tb.newTabSpec("id1"); 
     tab2 = tb.newTabSpec("id2"); 
     tab3 = tb.newTabSpec("id3"); 

     mems = new Intent(context,MemberList.class); 
     mems.putExtra("grpid", groupid); 
     mems.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     translist = new Intent(context,TransactionList.class); 
     translist.putExtra("grpid", groupid); 
     translist.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     settleamt = new Intent(context,SettleAmount.class); 
     settleamt.putExtra("grpid", groupid); 
     settleamt.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     View layview = LayoutInflater.from(context).inflate(R.layout.tab_buttonbg, null); 
     Button bt = (Button) layview.findViewById(R.id.tabuton); 
     bt.setText("Members"); 
     tab1.setIndicator(layview); 
     tab1.setContent(mems); 

     layview = LayoutInflater.from(context).inflate(R.layout.tab_buttonbg, null); 
     Button bt1 = (Button) layview.findViewById(R.id.tabuton); 
     bt1.setText("Transactions"); 
     tab2.setIndicator(layview); 
     tab2.setContent(translist); 

     layview = LayoutInflater.from(context).inflate(R.layout.tab_buttonbg, null); 
     Button bt2 = (Button) layview.findViewById(R.id.tabuton); 
     bt2.setText("Settle Amt"); 
     tab3.setIndicator(layview); 
     tab3.setContent(settleamt); 

     tb.getTabWidget().setDividerDrawable(R.drawable.divider); 

     tb.addTab(tab1); 
     tb.addTab(tab2); 
     tb.addTab(tab3); 

     tb.setCurrentTab(1); 

     v_createtrans.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v){ 
       Intent newtrans = new Intent(context,NewTransaction.class); 
       newtrans.putExtra("grpid", groupid); 
       startActivity(newtrans); 
      } 
     }); 

    } 

和佈局的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/grey_color"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="@color/app_header_color"> 

     <ImageButton 
      android:id="@+id/x_createtrans" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/addbtn" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="10dp" 
      android:background="@null"> 
     </ImageButton> 

    </RelativeLayout> 

    <TabHost 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@android:id/tabhost"> 

     <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#ffffff"> 
      </TabWidget> 

      <FrameLayout 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       android:id="@android:id/tabcontent"> 
      </FrameLayout> 

     </LinearLayout> 

    </TabHost> 

</LinearLayout> 

回答

0

嘗試使用

TabHost.SetOnTabChangedListener(this){ 
protected void OnTabChanged() 
{ 

} 
} 
+0

在這種方法中,我們怎麼能知道哪個選項卡已被選定/改變了嗎?你能給我一個示例代碼嗎? – 2012-04-20 14:17:13

+0

看到這個http://stackoverflow.com/questions/2243360/how-to-use-tabhost-ontabchangelistener-in-android – 2012-04-20 14:35:49

+0

謝謝。它的工作很棒。我們可以通過Tab鍵訪問TabActivity中的按鈕嗎?我的意思是,如果我們單擊TabActivity中的按鈕,則應該在選項卡內執行一些操作。 – 2012-04-20 15:27:24