My layout contain one header in which i inculded in each Activity, in this header there is a imag开发者_如何学运维e button. Is it possible to write a common onClick event for this imageButton??
You can write a class that extends OnClickListener and the onClick method. Then in each activity's onCreate method, find the ImageButton and set its onClickListener to that class:
MyOnclickListener implements OnClickListener {
private Context context;
public MyOnclickListener(Context context) {
this.context = context;
}
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, MyActivity.class);
context.startActivity(intent);
}
}
In your activities:
protected void onCreate(...) {
setContentView(...);
((ImageButton) findViewById(R.id.mybutton)).setOnClickListener(new MyOnclickListener(this));
}
EDIT: Sorry, of course implements.
EDIT2: See updated code for Context reference.
Yes. Create a singleton class that implements the required listener and add that instance to the button on each screen.
The answers provided by cant0na and Juhani are most likely the answers you are looking for (with a small note on cant0na's answer). If you need a more self-maintained and fault tolerant solution you can define your very own "widget" which handles its own events. For this you'll need:
- A xml-layout file which will describe your header.
- A custom class which will (automatically) inflate the above XML layout and manage any "common events".
The benefit of this solution is that you don't have to add a new instance of your common OnClickListener in each and every activity which will show your header. You simply add your header to your activitys layout-XML (see example code below) and nothing else. Foolproof. You also get a more "decoupled" code this way (your header doesn't depend on any implementation specifics of your application and its activities).
The drawback is that it's a more complex solution and it might seem a bit over-kill for small projects. It's also a bit tricky to keep this solution "decoupled" if you want to do any activity specific actions on the button click. You might want to consider "default behaviour" in combination with "code injection" in the MyHeader
class. The code injection would then require further manipulation on the header class (inject the onClick implementation) in the activities which deviates from the default behaviour.
Example header.xml
<com.dbm.widget.MyHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="60dp"
android:layout_height="20dp"
android:src="@drawable/myIcon"
android:id="@+id/myButton" />
</com.dbm.widget.MyHeader>
Example MyHeader.java
package com.dbm.widget;
public class MyHeader extends LinearLayout implements OnClickListener {
// Constructor.
public MyButton() {
((ImageButton) findViewById(R.id.myButton)).setOnClickListener(this);
}
// OnClick event callback.
public void onClick(View view) {
// Do whatever you need to do here.
}
}
Example activity.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.dbm.MyHeader
android:layout_width="match_parent"
android:layout_height="20dp" />
<!-- Your other content goes here -->
</LinearLayout>
精彩评论