[英]“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个不同的屏幕:
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.