包含的布局中的按钮 Onclick 侦听器 [英] Button Onclick Listener in included layouts

查看:19
本文介绍了包含的布局中的按钮 Onclick 侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我屈膝来找你,手头有问题.我对 Android 比较陌生,所以请原谅我可能说的任何亵渎神明的事情.

I come to you on bended knee, question in hand. I am relatively new to Android, so pardon any sacrilegious things I might say.

简介:我在应用中有多个布局,所有布局都必须包含一个共同的页脚.这个页脚有一些必要的按钮,用于返回主页、退出等.

Intro: I have several layouts in the app, that all have to include a common footer. This footer has some essential buttons for returning to the home page, logging out, etc.

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

I managed to get this footer to appear in all the requisite pages with the help of the Include and Merge tags. The issue lies in defining on click listeners for all the buttons. Although I can define the listeners in every activity associated with screens that include the footer layout, I find that this becomes terribly tedious when the number of screens increases.

我的问题是:我可以定义一个可以在整个应用程序中工作的按钮点击侦听器吗,可以使用 android:onClick 按钮的属性?

My question is this: Can I define a button click listener that will work across the application, which can be accessed from any screen with the use of the android:onClick attribute of the Button?

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

That is to say, I would like to define the button click listener once, in a separate class, say FooterClickListeners, and simply name that class as the listener class for any button clicks on the footer. The idea is to make a single point of access for the listener code, so that any and all changes to said listeners will reflect throughout the application.

推荐答案

我在几个布局中使用的菜单遇到了同样的问题.我通过在扩展 RelativeLayout 的类中扩展布局 xml 文件解决了这个问题,然后我在其中定义了 onClickListener.之后,我在需要菜单的每个布局中都包含了该类.代码如下所示:

I had the same problem with a menu which I used in several layouts. I solved the problem by inflating the layout xml file in a class extending RelativeLayout where I then defined the onClickListener. Afterwards I included the class in each layout requiring the menu. The code looked like this:

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

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>

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

with the <com.example.inflating.MenuView /> tag, you are now able to reuse your selfwritten Layout (incl onClickListener) in other layouts.

这篇关于包含的布局中的按钮 Onclick 侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆