[英]how to use two different OnClickListener in same activity?
I need to import following two different library 我需要导入以下两个不同的库
import android.content.DialogInterface.OnClickListener;
import android.view.View.OnClickListener;
as I want both DialogInterface.OnClickListener
and View.OnClickListener
in the same activity!! 因为我希望
DialogInterface.OnClickListener
和View.OnClickListener
在同一活动中!
how can I handle this?!! 我该如何处理呢?
because when I use both onClickListener
I got an error! 因为当我同时使用两个
onClickListener
出现错误!
Is it possible to have two different onClickListener
in one class?!! 一个类中可以有两个不同的
onClickListener
吗?!
Import just android.content.DialogInterface
and android.view.View
. 仅导入
android.content.DialogInterface
和android.view.View
。 Then you'll be able to reference the different OnClickListener classes by qualifying them with the parent class, as in 然后,您可以通过使用父类限定它们来引用不同的OnClickListener类,如
DialogInterface.OnClickListener foo = ...;
// and
View.OnClickListener bar = ...;
You're lucky, in this case, because the two classes with the same name happen to be nested classes. 在这种情况下,您很幸运,因为名称相同的两个类恰好是嵌套类。 If they weren't - the only difference being the package name, you'd have to use the fully-qualified class name of at least one of them:
如果不是,则唯一的区别是软件包名称,您必须使用其中至少一个的标准类名称:
android.content.DialogInterface.OnClickListener foo = ...;
OnClickListener bar = ...;
// or
OnClickListener foo = ...;
android.view.View.OnClickListener bar = ...;
// or
android.content.DialogInterface.OnClickListener foo = ...;
android.view.View.OnClickListener bar = ...;
which is neither fun to read nor write. 读和写都不有趣。
one way to go could be 一种方式可能是
public class MyClass implements DialogInterface.OnClickListener, View.OnClickListener {
}
你可以导入一个呼叫下一个onClickListener
如说Dialog.onClickListener
Make your class implement the interfaces 使您的类实现接口
extends Activity implements View.OnClickListener,DialogInterface.OnClickListener
Then 然后
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
@Override
public void onClick(View v) {
// dosomething
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.