问题
I have 7 buttons in all my 6 activities. All 6 buttons have the same functionality in all activities. How can I perform a common click event lisnter for these 6 buttons.
回答1:
You can create a new class that implements View.OnClickListener like this:
public class MyClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
// TODO handle the click
}
}
In all your activities you can then set the click listener like this:
button.setOnClickListener(new MyClickListener());
You could even save the context in the class for displaying Toasts etc.
public class MyClickListener implements View.OnClickListener {
private Context context;
public MyClickListener(Context context) {
this.context = context;
}
@Override
public void onClick(View view) {
Button button = (Button) view;
Toast.makeText(this.context, button.getText().toString(), Toast.LENGTH_SHORT).show();
}
}
回答2:
Have a class like this:
public class OnClickMaker{
public static View.OnClickListener getOnClick(){
return new View.OnClickListener(){
public void on click(View v){
//do stuff
}
};
}
}
And then in your other class, do this
button.setOnClickListener(OnClickMaker.getOnClick());
回答3:
Putting more words in Sagar's answer given above.
Assumption:
As you have said you have 7 buttons in your 6 activities, I assume all the 7 buttons have same functionality/code.
Solution:
Step 1: Include android:click="btnFirstClick"
in <Button>
inside your XML layouts.
Step 2: Define abstract BaseActivity class by extending Activity and include methods with the same name you have given in android:onClick
attribute.
abstract public class BaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void btnFirstClick(View v) {
}
public void btnSecondClick(View v) {
}
.....
.....
// same for other buttons
}
Step 3: Now extends this BaseActivity to all your 6 activities.
For example:
public class FirstActivity extends BaseActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_first);
}
}
回答4:
Make a Function in a Class and put that class in the same package where all other classes are there. Simply just call that function in 6 onclicklisteners .
回答5:
You can do this,
Put the click events in the XML files android:click="clickMe"
Create this function in a Activity
say
public void clickMe(View view) {
...//You handling code
}
and extend this Activity by all your activities.
回答6:
You can do like following
create a base activity
class BaseActivity extends Activity{
public void onSpecificEvent(View v){
// do your tasks
}
}
now extends your activities from this one
class Activity1 extends BaseActivity
class Activity2 extends BaseActivity
In which buttons you need to implement the onClick use following in xml
android:onClick="onSpecificEvent"
回答7:
If you are in the same situation like me, I have a linear layout with undetermined number of buttons in it (generated dynamically) and you want a common listener for all of the button (log out their text for example), you can do like this
for (int i = 0; i < linearLayout.getChildCount(); i++)
{
final Button b = (Button) linearLayout.getChildAt(i);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Commons.log(b.getText().toString());
//your other code here
}
});
}
Now all the children (buttons) of the linear layout have same click listener
来源:https://stackoverflow.com/questions/16870149/common-class-for-click-listener