setOnCheckedChangeListener参数 [英] setOnCheckedChangeListener arguments
问题描述
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.orientation);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.horizontal:
group.setOrientation(LinearLayout.HORIZONTAL);
break;
case R.id.vertical:
group.setOrientation(LinearLayout.VERTICAL);
break;
}
}
});
什么是radioGroup.setOnCheckedChangeListener?这是一种方法吗?那么它的论点是什么?
What is radioGroup.setOnCheckedChangeListener eactly? Is it a method? What are it's arguments then?
推荐答案
setOnCheckedChangeListener()
是一种方法它设置一个监听器,用于监听特定的操作。
setOnCheckedChangeListener()
is a method which sets a listener which listens on a specific action.
这个想法是这样的:
-
你想要单选按钮当用户更改选择时执行某些操作。
You want the radio button to perform some action when the user changes the selection.
只要选择更改,系统就会告诉单选按钮。如果单选按钮具有侦听器集,则侦听器侦听该事件 - 有一种机制通过调用其方法执行某些操作 onCheckedChanged()
The system tells the radio button whenever the selection has been changed. If the radio button have a listener set, the listener "listens" to the event - there is a mechanism which performs some action by calling its method onCheckedChanged()
您应该意识到事件发生时会调用 onCheckedChanged()
,而不是在侦听器时传递给 setOnCheckedChangeListener()
方法 - 所以在调用代码示例时不会!
You should realize that the onCheckedChanged()
is called in the moment the event happens, not at the time the listener is passed to the setOnCheckedChangeListener()
method - so not at the time of your code example is called!
类新的RadioGroup.OnCheckedChangeListener()
需要传递监听器的行为 - 最多包含Java 7,你不能只传递一个方法没有上课。从Java 8开始,您可以使用 lambda表达式轻松完成它。 ,但现在不要理会它。
The class new RadioGroup.OnCheckedChangeListener()
is needed to pass the listener's behaviour - up to Java 7 inclusive, you cannot pass just a method without a class. Since Java 8 you may do more easily it using lambda expressions, but don't bother with it now.
这是一个非常高级的话题,我建议你研究这个第一个:
This is quite an advanced topic, I recommend you to study this first:
- Listener design pattern (a.k.a. Observer)
- Anonymous classes
您可以用简化的方式考虑它。您的代码具有以下含义:
You may think about it in the simplified way. Your code has the following meaning:
亲爱的
radioGroup
,
$ b每当检查更改时你应该做的事情是:
Dear
radioGroup
,
the thing which you should do whenever the check has been changed is this:
- 如果选择水平,
致电group.setOrientation(LinearLayout.HORIZONTAL)
- 如果选择垂直,
电话group.setOrientation(LinearLayout.VERTICAL)
- if "horizontal" has been selected,
callgroup.setOrientation(LinearLayout.HORIZONTAL)
- if "vertical" has been selected,
callgroup.setOrientation(LinearLayout.VERTICAL)
NB 不要这样做现在,只有当检查更改时才
这篇关于setOnCheckedChangeListener参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!