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

查看:139
本文介绍了按钮的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.

我设法得到这个页脚出现在所有必要的网页与包括与合并标签的帮助。问题在于定义的点击监听所有按钮。虽然我可以定义在每一个与屏幕,包括页脚布局关联的活动的听众,我觉得这将成为非常繁琐的,当银幕数量增加了。

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.

我的问题是这样的:我可以定义一个按钮,点击监听器,将在应用程序工作,这可以从任何屏幕访问与使用的安卓的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 的,并简单地命名的类作为监听器类的任意按钮点击页脚。我们的想法是做一个单点访问听者code的,这样就表示,任何和所有的变化听众将整个应用程序中反映。

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.

推荐答案

我有同样的问题,这在我以前的几个布局菜单。我通过膨胀的布局XML文件中的一类延伸RelativeLayout的,我再定义的onClickListener解决了这个问题。后来我包括类中的每个版面需要的菜单。在code是这样的:

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>

&LT; com.example.inflating.MenuView /&GT; 标签,你现在可以重用selfwritten布局(包括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天全站免登陆