简体   繁体   中英

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!!

how can I handle this?!!

because when I use both onClickListener I got an error!

Is it possible to have two different onClickListener in one class?!!

Import just android.content.DialogInterface and android.view.View . Then you'll be able to reference the different OnClickListener classes by qualifying them with the parent class, as in

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM