简体   繁体   English

Android:如何基于选项卡选择更改TabActivity中的ImageButton

[英]Android: How to Change an ImageButton in TabActivity based on tab selection

I have the following TabActivity 我有以下TabActivity

在此处输入图片说明

Now when I select "Settle Amt" tab , the imagebutton on the top should change to another ImageButton, and when "Members" tab is selected, no button should be displayed. 现在,当我选择“ Settle Amt”选项卡时,顶部的图像按钮应更改为另一个ImageButton,并且当选择“ Members”选项卡时,不应显示任何按钮。 ImageButton is in the TabActivity.How can I achieve button change dynamically based on selecting the tabs? ImageButton位于TabActivity中。如何在选择标签的基础上动态实现按钮更改?

Here is my 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);
            }
        });

    }

and layout xml : 和布局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>

try to use 尝试使用

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

 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM