"页眉和QUOT;视图和按钮:我怎么重视听众在&QUOT按钮,标题和QUOT;不具有其自身的活动? [英] "Header" Views and buttons: how do I attach listeners to Buttons in a "header" that does not have its own Activity?
问题描述
嘿, 我已经谈到了这个问题,<一个href="http://stackoverflow.com/questions/2013433/whats-the-right-choice-create-new-activities-or-just-create-a-different-layout-a">here,克里斯托弗给了一个答案,但我真的不得到它,所以我认为它的时间,使之成为真正的问题,而不仅仅是一个跟进=)
既然这样,应用程序我写有4个不同的画面: 1.屏幕1 - 节点列表(主屏幕) 2.屏幕2 - 选项菜单,tableLayout的按钮 3.屏幕3 - 导航 4.屏幕4 - 对版本的文字信息等。
这些屏幕可以从利用头查看被放置在顶部导航到/。头则有4个不同的按钮:
+ -------------------- +
|菜单与按钮|
+ -------------------- +
| |
| |
| |
| C 0 NT·简T |
| |
| |
| |
+ -------------------- +
头只是一个XML文件(header.xml)有几个按钮。这header.xml是使用包括-标记的包含在布局。例如,main.xml中具有行:
&LT;包括布局=@布局/头&GT;&LT; /包括&GT;
标题显示正常的,但问题是 - 什么是附加OnClickListeners在头的按钮正确的做法
?克里斯托弗·<一个href="http://stackoverflow.com/questions/2013433/whats-the-right-choice-create-new-activities-or-just-create-a-different-layout-a">pointed出,你可以创建一个Activity类,做钩子那里,像这样的:
公共类BaseActivity延伸活动{
保护View.OnClickListener mButtonListener;
保护无效setupHeaderButtons(){
findViewById(R.id.header_btn_1).setOnClickListener(mButtonListener);
// ...
findViewById(R.id.header_btn_n).setOnClickListener(mButtonListener);
}
}
公共类FirstActivity扩展BaseActivity {
@覆盖
公共无效的onCreate(包B){
super.onCreate(B);
的setContentView(R.layout.first_activity);
//这需要做*查看已膨胀后
setupHeaderButtons();
}
}
首先,我不能让,因为该方法它的工作 setupHeaderButtons 的心不是从FirstActivity访问。 其次,这是正确的方式去,好吗?
问候
在 setupHeaderButtons()
方法保护
所以只能通过类延长
基类,这是 BaseActivity
在这种情况下。
您确认您的 FirstActivity
正在扩大 BaseActivity
?
Hey, I have touched on this question here, where Christopher gave an answer to this, but I dont really get it so I thought its time to make it a real question, not just a "follow up" =)
As it stands, the application Im writing has 4 different screens: 1. Screen 1 - list of nodes (main screen) 2. Screen 2 - options menu, tableLayout with buttons 3. Screen 3 - navigation 4. Screen 4 - text details on version etc
These screens can be navigated to/from using a "header" View that is placed on top. the header then has 4 different buttons:
+--------------------+
| menu with buttons |
+--------------------+
| |
| |
| |
| C O N T E N T |
| |
| |
| |
+--------------------+
The header is just an XML-file (header.xml) with a few buttons. That header.xml is the included in the Layouts using the include-markup. For example, the main.xml has the line:
<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?
Christopher pointed out that you could create an Activity class and do the hooks there, like this:
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 cant make it work since the method setupHeaderButtons isnt accessible from FirstActivity. Secondly, is this the right way to go at it?
Regards
The setupHeaderButtons()
method is protected
so it can only be accessed by classes that extend
that base class, which is BaseActivity
in this case.
Are you sure that your FirstActivity
is extending BaseActivity
?
这篇关于&QUOT;页眉和QUOT;视图和按钮:我怎么重视听众在&QUOT按钮,标题和QUOT;不具有其自身的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!