英文:
Significance of using ID in switch statements in onClick()
问题
以下是翻译好的部分:
我想要问的是,在为多个按钮实现onClickListener接口时,创建switch语句的意义是什么。就像我们已经通过说button_name.setOnClickListener()为特定的按钮调用了setOnClickListener一样。
那么在switch语句中再次指定id的目的是什么?这样做有必要吗?难道不是button_name.setOnClickListener()的意思是,“对于这个按钮,执行这里面的内容”吗?
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 从布局中获取我们的按钮
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// 使用上面的实现注册onClick监听器
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// 当按钮被点击时执行某些操作
// 是的,我们将在这里处理点击,但是点击了哪个按钮?我们不知道
// 所以我们会这样做
switch (v.getId() /*获取被点击视图的id*/) {
case R.id.corky:
// 当点击corky按钮时执行某些操作
break;
case R.id.corky2:
// 当点击corky2按钮时执行某些操作
break;
case R.id.corky3:
// 当点击corky3按钮时执行某些操作
break;
default:
break;
}
}
}
我想要问的是,使用button2.setOnClickListener()的意思是,“对于button2,执行这个函数设置的内容”,对吗?如果不是,那么setOnClickListener()的实际目的/功能是什么?
英文:
I wanted to ask what was the significance of creating a switch statement when implementing the onClickListener interface for multiple buttons. Like, we're already calling the setOnClickListener for that particular button by saying button_name.setOnClickListener()
So what's the point of specifying the id's again in the switch statement ? Why is it necessary to do so ? doesn't the point of doing button_name.setOnClickListener() mean - "do what's in here for this button" ?
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know
// So we will make
switch (v.getId() /*to get clicked view id**/) {
case R.id.corky:
// do something when the corky is clicked
break;
case R.id.corky2:
// do something when the corky2 is clicked
break;
case R.id.corky3:
// do something when the corky3 is clicked
break;
default:
break;
}
}
}
What I'm trying to ask is, doing button2.setOnCLickListener() means - "Do what's set by this function for button2" right ? if not then what is the actual purpose/function performed by setOnClickListener() ?
答案1
得分: 1
因为您已经为3个不同的按钮设置了clickListeners,所以当您点击其中任何一个按钮时,方法onClick(View v)
会被调用。如果您点击button1,实际上不希望执行为button3分配的任务。因此,为了检查并找出哪个按钮被按下,使用switch语句是必要的。
或者您也可以这样做:
corkyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// 当点击corky时执行某些操作
}
});
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// 当点击play时执行某些操作
}
});
依此类推...
这种方式不需要像上面的switch语句那样,因为在这种情况下,您知道正在被点击的是哪个按钮,因为您设置了clickListener,并且在其中指定了您想要在那里执行的操作。
英文:
Since you have set clickListeners to 3 different buttons, the method
....onClick(View v){
.....
is going to get called when you click any of those 3 buttons. If you click the button1 you do not want to perform task actually assigned for button3. So to check and find out which button was pressed the switch statement is necessary.
Alternatively you could have done this:
corkyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// do something when the corky is clicked
}
});
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
// do something when the play is clicked
}
});
so on and so forth...
This way you don't need a switch statement as in this case you know which button is being clicked on, AS you're setting the clickListener AND ALSO specifiying what you wanna do there itself.
答案2
得分: 1
这更像是一个清晰的外观,更可读的代码样式结构。如果您有多个需要点击侦听器的小部件,通过实现 View.OnClickListener 接口并覆盖 onClick 可以使代码看起来更干净。
在 onClick(View view) 内部:
- 您也可以使用 if 语句,但是当小部件数量较多时,代码会变得混乱,这就是为什么我们使用 switch 语句使其看起来更清晰。
然后
> btn.setOnClickListener(this)
// 告诉按钮传递该接口,以便覆盖的 onClick 可以侦听它。
但是,如果您不想这样做,或者只有一个需要点击的小部件,则只需使用此方法,无需实现和覆盖。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
英文:
It's more of a clean look, a more readable code style structure. If you have multiple widgets who need click listener, by implementing View.OnClickListener interface
and overriding onClick gives a cleaner look.
and inside onClick(View view){
- you can also use if statement but when the number of widgets is more
then it becomes messy, that's why we use switch statement to look cleaner.
}
and
> btn.setOnClickListener(this)
// telling the button to pass the interface
so that overridden onClick can listen to this.
But If you don't want that or you have only one widget for click
then just use this, no need to implement and override.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论