简体   繁体   English

如何在同一活动中使用两个不同的OnClickListener?

[英]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.OnClickListenerView.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.DialogInterfaceandroid.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.

相关问题 如何在同一活动中使用不同的视图 - How to use different views in same activity 如何在两个Activity中使用相同的代码JSOUP - How to use same code JSOUP in two Activity 是否可以在同一Activity上将实例ValueEventListener与addValueEventListener一起用于两个不同的调用? - Is possible to use the instance ValueEventListener for two different calls with addValueEventListener, at same Activity? 如何在同一程序中使用onclick和onclicklistener? - How to use onclick and onclicklistener in same program? 如何在两个 CanvasView 对象上使用 onClickListener? - How to use onClickListener on two CanvasView Objects? 如何在Android中使用不同的参数在同一活动中裁剪两个不同的图像? - How to crop two different images in the same activity with different parameters in Android? 如何在 Android 中使用相同的 Edittext 作为两个不同的? - How to use same Edittext as two different in Android? 如何在两个不同的类中使用相同的计数器 - How to use the same counter in two different classes 在不同的活动中使用相同的“ OnClickListener” - Using the same 'OnClickListener' in different Activities 如何在同一活动中使用两种Web服务方法? - How can i use two web service methods in the same activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM