onclick听众(这个)是什么意思? [英] What does the onclick listener (this) mean?
问题描述
btn_Login.setOnClickListener(this);
在android studio中。
In android studio.
我'在无数的地方见过这个。这是什么意思?我知道这个
是如何运作的,但是那个叫做的监听器是什么?
I've seen this in countless places. What does this mean? I know how this
operates but what is the listener called then?
此示例来自:链接
推荐答案
假设您有16个按钮且每个按钮都有 setOnclicklistener
这些方法您正在课堂上创建许多类似代码的重复。这会使你的代码变得丑陋,这也不是编写代码的有效方法。因此,要使代码高效,您必须在活动上实现 OnClickListener()
,然后对每个按钮使用 buttonX.setOnClickListener(this)
。现在使用覆盖 onClick
方法。在此方法中,您可以使用Switch case块或if else来识别按下哪个按钮。所以在 onClick
方法中你只需要给出按钮的id。
Suppose that you have 16 Buttons and every button has setOnclicklistener
these means that you are creating many repetitions of similar code to this in your class. And that makes your code ugly, also this is not the efficient way to write your code. So to make your code efficient you have to implement OnClickListener()
on your activity and then for each button use buttonX.setOnClickListener(this)
. Now use the override onClick
method. In this method you can use either Switch case block or if else to identify which button is pressed. So in onClick
method you just have to give id's of the button.
实现 OnClickListener
in Activity
Implement OnClickListener
in Activity
public class MyActivity extends Activity implements View.OnClickListener {
}
对于每个按钮,请使用:
For each button use this:
buttonX.setOnClickListener(this);
此工具覆盖 onClick
方法
@Override
public void onClick(View view) {
switch(view.getId){
case R.id.buttonX:
// Do something
}
这篇关于onclick听众(这个)是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!