简体   繁体   English

“标题”视图和按钮:如何在没有自己的Activity的“标题”中将监听器附加到按钮?

[英]“Header” Views and buttons: how do I attach listeners to Buttons in a “header” that does not have its own Activity?

I have touched on this question here , where Christopher gave an answer to this, but I don't really understand it, so I thought it's time to make it a real question, not just a "follow up" =) 我在这里提到了这个问题,克里斯托弗在这里给出了答案,但我并不是真的理解它,所以我认为是时候把它作为一个真正的问题,而不仅仅是“跟进”=)


As it stands, the application I'm writing has 4 different screens: 就目前而言,我正在编写的应用程序有4个不同的屏幕:

  1. Screen 1 - list of nodes (main screen) 屏幕1 - 节点列表(主屏幕)
  2. Screen 2 - options menu, tableLayout with buttons 屏幕2 - 选项菜单,带有按钮的tableLayout
  3. Screen 3 - navigation 屏幕3 - 导航
  4. Screen 4 - text details on version etc 屏幕4 - 版本等的文本详细信息

These screens can be navigated to/from using a "header" View that is placed on top. 可以使用放置在顶部的“标题”视图来导航这些屏幕。 The header then has 4 different buttons: 然后标题有4个不同的按钮:

+--------------------+
| menu with buttons  |
+--------------------+
|                    |
|                    |
|                    |
|  C O N T E N T     |
|                    |
|                    |
|                    |
+--------------------+

The header is just an XML-file (header.xml) with a few buttons. 标题只是一个带有几个按钮的XML文件(header.xml)。 That header.xml is the included in the Layouts using the include-markup. 该header.xml是使用include-markup包含在Layouts中的。 For example, the main.xml has the line: 例如,main.xml包含以下行:

<include layout="@layout/header"></include>

The header show up alright, but the question is - what is the correct approach to attach OnClickListeners for the buttons in the header? 标题显示正常,但问题是 - 为标题中的按钮附加OnClickListeners的正确方法是什么?

Christopher pointed out that you could create an Activity class and do the hooks there, like this: 克里斯托弗指出你可以创建一个Activity类并在那里做钩子,如下所示:

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);

        // This needs to be done *after* the View has been inflated
        setupHeaderButtons();
    }
}

First, I can't make it work since the method setupHeaderButtons isn't accessible from FirstActivity. 首先,我不能让它工作,因为该方法setupHeaderButtons不是来自FirstActivity访问。 Secondly, is this the right way to go at it? 其次,这是正确的方法吗?

The setupHeaderButtons() method is protected so it can only be accessed by classes that extend that base class, which is BaseActivity in this case. setupHeaderButtons()方法protected因此只能由extend该基类的类访问,在本例中为BaseActivity

Are you sure that your FirstActivity is extending BaseActivity ? 您确定您的FirstActivity正在扩展BaseActivity吗?

I would prefer this so you don't have to remember (and probably forget) to invoke setupHeaderButtons for every derived Activity. 我更喜欢这样,所以你不必记住(并且可能忘记)为每个派生的Activity调用setupHeaderButtons。 BTW, set U pHeaderButtons it's a better name. 顺便说一句,设置U pHeaderButtons这是一个更好的名字。

public class BaseActivity extends Activity {
    protected View.OnClickListener mButtonListener;

@Override
public void setContentView(int layoutResID) {
    super.setContentView(layoutResID);
            setupHeaderButtons();
    }

    protected void setupHeaderButtons() {
        findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
        // ...
        findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
    }
}

public class FirstActivity extends BaseActivity {
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.first_activity);
    }
}

I personally don't think you should overcomplicate things. 我个人认为你不应该过分复杂化。 Having to call setupHeaderButtons should be fine, especially if you only have a handful of activities. 必须调用setupHeaderButtons应该没问题,特别是如果你只有一些活动。

If you're using standard launch modes, the activity will be relaunched. 如果您使用的是标准启动模式,则会重新启动该活动。 Check out Application Fundamentals if you're interested in learning about launch modes. 如果您有兴趣了解启动模式,请查看应用基础知识

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Button在LWUIT中没有动作侦听器而命令没有? - Why do Buttons have action listeners and commands don't in LWUIT? 如何在一个活动中具有多个按钮 - How to have multiple buttons in one activity 我有 5 个按钮我想更改按钮的 mediaPlayer 源 fonClick 怎么做? - I have 5 buttons I want to change mediaPlayer source fonClick of the buttons how to do that? 如何让程序用Java创建自己的变量? - How do I have a program create its own variables with Java? 我在Android Studios上以XML实现了卡片视图。 我该如何充当按钮? - I implemented card views in XML on Android Studios. How can I have this act as buttons? 将主要活动中的第一个按钮链接到网站后,如何将按钮链接到第二个活动? - how do i link a button to a second activity after linking the first buttons in my main activity to websites? JTable标头包含图像,在该图像的顶部,我想在单个标头中放置3个按钮 - JTable Header contains Image and on top of that image I want to place 3 buttons in a single header 如何在Android Studio中有效地将属性更改为多个视图/按钮? - How do I efficiently change a property to multiple views/buttons in Android Studio? 我在测试中有5个选择。 我有一个csv数据库。 当我按下其中一个按钮时,我希望按钮上的“文本”更改为下一个标题 - I have 5 choices in a test. I have a csv database. When I press on one of the buttons, I want the Text on the button to change to the next header 动态按钮上的事件侦听器 - Event Listeners on Dynamic Buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM