2010-11-08 42 views
20

我彎下腰來問你,問題在手。我對Android比較陌生,因此請原諒我可能會說的任何褻瀆神明的事情。按鈕onclick聽衆在包含的佈局

簡介:我在應用程序中有幾個佈局,都必須包含一個通用的頁腳。這個頁腳有一些重要的按鈕,用於返回到主頁,註銷等。

我設法讓這個頁腳出現在所有必要的頁面中,藉助Include和Merge標籤。問題在於爲所有按鈕定義點擊監聽器。儘管我可以在與包含頁腳佈局的屏幕相關的每個活動中定義聽衆,但我發現當屏幕數量增加時,這變得非常乏味。

我的問題是這樣的:我可以定義一個按鈕,點擊收聽,這將在應用程序工作,這可以從任何屏幕,使用Android的的訪問:按鈕的的onClick屬性?

也就是說,我想在一個單獨的類中定義一次按鈕點擊偵聽器,如FooterClickListeners,並簡單地將該類命名爲監聽器類,以便在頁腳上單擊任何按鈕。這個想法是爲偵聽器代碼創建一個單一的訪問點,以便對所有偵聽器進行的任何和所有更改都會反映到整個應用程序中。

回答

16

我和我在幾種佈局中使用的菜單有同樣的問題。我通過在擴展了RelativeLayout的類中擴展布局xml文件來解決問題,然後在那裏定義了onClickListener。之後,我將課程包含在每個需要菜單的佈局中。該代碼是這樣的:

menu.xml文件

<?xml version="1.0" encoding="utf-8"?> 

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageButton android:id="@+id/map_view" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/button_menu_map_view" 
     android:background="@null" 
     android:scaleType="fitCenter" 
     android:layout_height="@dimen/icon_size" 
     android:layout_width="@dimen/icon_size"> 
    </ImageButton> 

    <ImageButton android:id="@+id/live_view" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:src="@drawable/button_menu_live_view" 
     android:background="@null" 
     android:scaleType="fitCenter" 
     android:layout_height="@dimen/icon_size" 
     android:layout_width="@dimen/icon_size"> 
    </ImageButton> 

    <ImageButton android:id="@+id/screenshot" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/button_menu_screenshot" 
     android:background="@null" 
     android:scaleType="fitCenter" 
     android:layout_height="@dimen/icon_size" 
     android:layout_width="@dimen/icon_size"> 
    </ImageButton> 

</merge> 

MenuView.java

public class MenuView extends RelativeLayout { 

    private LayoutInflater inflater; 

    public MenuView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.menu, this, true); 

     ((ImageButton)this.findViewById(R.id.screenshot)).setOnClickListener(screenshotOnClickListener);   
     ((ImageButton)this.findViewById(R.id.live_view)).setOnClickListener(liveViewOnClickListener);  
     ((ImageButton)this.findViewById(R.id.map_view)).setOnClickListener(mapViewOnClickListener); 
    } 

    private OnClickListener screenshotOnClickListener = new OnClickListener() { 
     public void onClick(View v) { 
      getContext().startActivity(new Intent(getContext(), ScreenshotActivity.class)); 
     } 
    }; 

    private OnClickListener liveViewOnClickListener = new OnClickListener() { 
     public void onClick(View v) { 
      getContext().startActivity(new Intent(getContext(), LiveViewActivity.class)); 
     } 
    }; 

    private OnClickListener mapViewOnClickListener = new OnClickListener() { 
     public void onClick(View v) { 
      getContext().startActivity(new Intent(getContext(), MapViewActivity.class)); 
     } 
    }; 
} 

佈局。XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <SurfaceView android:id="@+id/surface" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 

    </SurfaceView> 

    <!-- some more tags... --> 

    <com.example.inflating.MenuView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

</RelativeLayout> 

<com.example.inflating.MenuView />標籤,你現在可以在其他佈局重用你selfwritten佈局(包括onClickListener)。

+0

謝謝比利天才:)它的工作原理! – 2011-01-10 06:39:40

+0

非常感謝。這個對我有用!!! – AD14 2013-06-11 16:39:18

+1

不錯的解決方案,但有點長。如果您正在尋找一些簡單的解決方案,請參閱http://stackoverflow.com/a/16870468/1055241 – gprathour 2014-06-26 09:00:03

0

您所描述的解決方案是不可能的,對不起。但是,對於使用頁腳的所有活動,您可以擁有共同的父項活動。在該活動中,只需爲頁腳按鈕提供處理程序方法,然後每次需要處理頁腳操作時都從其繼承。

+0

感謝您的快速回復。是的,我曾想過你建議的解決方案,但希望找到我所描述的解決方案。我想我必須選擇重複代碼和繼承。再次感謝! – 2010-11-08 07:52:45

+0

這裏是你不能實現這個作爲一個tabview的原因嗎? – Falmarri 2010-11-08 07:57:39

+0

嗨,Falmarri,tabview似乎有一個問題,並崩潰的應用程序,所以我不得不訴諸這種形式。 – 2010-11-10 05:27:32

3

這是在未來將被添加到roboguice的東西。它將允許您爲標題欄和頁腳等內容構建控制器類,併爲您自動裝配事件。

簽出http://code.google.com/r/adamtybor-roboguice/爲最初的秒殺。

基本上,如果您使用的是roboguice,您可以爲頁腳定義一個組件,並將該頁腳組件注入到每個活動中。

不幸的是,您仍然必須將控制器添加到每個活動中,就像您使用包含佈局一樣,但好消息是所有的東西都會爲您和您的所有邏輯保持在一個類中。

下面是一些示例用法的一些僞代碼。

public class FooterController { 
    @InjectView(R.id.footer_button) Button button; 

    @Inject Activity context; 

    @ContextObserver 
    public void onViewsInjected() { 
    button.setOnClickListener(new OnClickListener() { 
     void onClick() { 
     Toast.makeToast(context, "My button was clicked", Toast.DURATION_SHORT).show(); 
     } 
    }); 
    } 
} 

public class MyActivity1 extends RoboActivity { 
    @Inject FooterController footer; 
} 

public class MyActivity2 extends RoboActivity { 
    @Inject FooterController footer; 
} 
+0

看起來很甜!謝謝! – 2011-06-27 09:57:51