开发者

how to customize class for imagebutton panel?

i have made one simple button panel. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="bottom" android:id="@+id/buttonpanel"> <ImageButton android:id="@+id/buttonhome" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/homeselector" android:focusable="true"> </ImageButton> <ImageButton android:id="@+id/buttonsearch" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/searchselector" android:focusable="true"> </ImageButton>> <ImageButton android:id="@+id/buttonreg" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/registerselector" android:focusable="true"> </ImageButton>> <ImageButton android:id="@+id/buttonlogin" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/loginselector" android:focusable="true"> </ImageButton> </LinearLayout>

Now my problem is that i am using it in my entire app.suppose i have 4 activity class that includes this panel.

so i have to write th开发者_JAVA百科e below code number of times as activity classess.

buttonhome=(ImageButton)findViewById(R.id.buttonhome);
    buttonhome.setOnClickListener(this);

    buttonsearch=(ImageButton)findViewById(R.id.buttonsearch);
    buttonsearch.setOnClickListener(this);

    buttonreg=(ImageButton)findViewById(R.id.buttonreg);
    buttonreg.setOnClickListener(this);

    buttonlogin=(ImageButton)findViewById(R.id.buttonlogin);
    buttonlogin.setOnClickListener(this);

}
public void onClick(View view)
{
    int id=view.getId();
    switch (id) {
    case R.id.buttonhome:
        break;
    case R.id.buttonsearch:
        break;
    case R.id.buttonreg:
        break;
    case R.id.buttonlogin:
        finish();
        startActivity(new Intent("com.coupon.main.couponmandi.Login"));
        break;
    }

so is there any way that i can make one single class that handles all things.

thankx


Create a class that implements the clicklistener, and put all the above codes there. In each activity, construct that class.

public class ButtonHandler implements OnClickListener{
    private Activity mContext;
    public ButtonHandler(Activity context){
       mContext=context;

       buttonhome=(ImageButton)mContext.findViewById(R.id.buttonhome);
       buttonhome.setOnClickListener(this);
       ...
    }

    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜