[英]Capture click in any button with android
Who to capture the click of any button? 谁来捕捉任何按钮的点击?
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(WHKConversorActivity.this, "Hello World", Toast.LENGTH_SHORT).show();
}
});
this is a click on button1 but i need an function for all buttons :-/ . 这是一个对button1的单击,但是我需要所有按钮的功能:-/。 Example in javascript:
JavaScript中的示例:
$('button').click(function(){
alert($(this).val());
});
Thanks :) 谢谢 :)
If you want all your buttons to do exactly the same thing (which is unlikely) you could bind the same click listener to each button. 如果您希望所有按钮执行完全相同的操作(这不太可能),则可以将相同的单击侦听器绑定到每个按钮。 Eg:
例如:
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mGlobalClick);
final Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(mGlobalClick);
... ect
OnClickListener mGlobalListener = new OnClickListener() {
@Override
public void onClick(View v) {
//Stuff
}
};
Or if you want a each button to do something different, but still need some sort of reapeated function for each, you could put that part of the code in a seperate method and reference it in every click listener 或者,如果您希望每个按钮执行不同的操作,但每个按钮仍需要某种重新实现的功能,则可以将该部分代码放在单独的方法中,并在每个单击侦听器中引用它
public void GlobalStuff(){
//Stuff
}
OnClickListener mSpecificListener = new OnClickListener() {
@Override
public void onClick(View v) {
GlobalStuff();
//More Stuff
}
};
There is no generic way like in javascript. 没有像javascript这样的通用方法。 You need to assign the click handler to all buttons individually.
您需要将点击处理程序分别分配给所有按钮。
您可以尝试覆盖Button
类,并在其构造函数中调用addListener
。
Assuming all views in your activity are in some kind of ViewGroup, you can use this: 假设您活动中的所有视图都在某种ViewGroup中,则可以使用以下方法:
private void applyToAllButtons(ViewGroup viewGroup, OnClickListener listener) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
if (viewGroup.getChildAt(i) instanceof Button) {
viewGroup.getChildAt(i).setOnClickListener(listener);
}
}
}
Then, in your onCreate(), do the following: 然后,在您的onCreate()中,执行以下操作:
@Override
public void onCreate(Bundle savedInstanceState) {
... // old code here
View view = findViewById(R.id.layoutRoot);
setContentView(view);
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// Do stuff here.
}
};
applyToAllButtons((ViewGroup) view, listener);
}
Thanks Jack... this is more useful :) less variables, less memory. 谢谢杰克...这更有用:)减少变量,减少内存。 The OnClickListener put before tiggers:
OnClickListener放在老虎之前:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OnClickListener globalClick = new View.OnClickListener() {
public void onClick(View v) {
// some functions...
}
};
/* Tiggers */
findViewById(R.id.button1).setOnClickListener(globalClick);
findViewById(R.id.button2).setOnClickListener(globalClick);
findViewById(R.id.button3).setOnClickListener(globalClick);
findViewById(R.id.button4).setOnClickListener(globalClick);
findViewById(R.id.button5).setOnClickListener(globalClick);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.