2010-11-08 80 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