包含布局的按钮Onclick Listener

Sre*_*i J 22 layout android listeners include buttonclick

我弯着膝盖来到你面前,问题在手.我对Android比较陌生,所以请原谅我所说的任何亵渎神灵的事情.

简介:我在应用程序中有几个布局,所有布局都必须包含一个公共页脚.这个页脚有一些必要的按钮,用于返回主页,注销等.

在Include和Merge标签的帮助下,我设法让这个页脚出现在所有必需的页面中.问题在于为所有按钮定义单击侦听器.虽然我可以在与包含页脚布局的屏幕相关联的每个活动中定义侦听器,但我发现当屏幕数量增加时,这变得非常繁琐.

我的问题是:我可以定义一个按钮单击侦听器,它将在整个应用程序中工作,可以使用Button 的on:onClick属性从任何屏幕访问它吗?

也就是说,我想在单独的类中定义按钮单击侦听器一次,比如FooterClickListeners,并简单地将该类命名为页脚上任何按钮单击的侦听器类.我们的想法是为侦听器代码创建单一访问点,以便对所述侦听器的任何和所有更改都将反映在整个应用程序中.

bil*_*ted 18

我在几个布局中使用的菜单遇到了同样的问题.我通过在扩展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>
Run Code Online (Sandbox Code Playgroud)

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));
        }
    };  
}
Run Code Online (Sandbox Code Playgroud)

layout.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>
Run Code Online (Sandbox Code Playgroud)

使用<com.example.inflating.MenuView />标记,您现在可以在其他布局中重用自写布局(包括onClickListener).

  • 不错的解决方案,但有点长。如果您正在寻找一些简单的解决方案,请访问http://stackoverflow.com/a/16870468/1055241 (2认同)