2011-01-18 8 views
0

私は簡単なボタンパネルを1つ作った。 <?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>イメージボタンパネルのクラスをカスタマイズする方法は?

私の問題は、アプリケーション全体で使用していることです。このパネルを含む4つのアクティビティクラスがあります。

私は以下のコードをアクティビティのクラスとして書く必要があります。

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

ので、私はすべてのものを処理する1つのクラスを作ることができますどのような方法があります。

答えて

2

thankx

はclicklistenerを実装するクラスを作成し、そこに上記のすべてのコードを置きます。各アクティビティで、そのクラスを構築します。

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

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

    ... 
} 
関連する問題