DialogInterface VS查看OnClickListeners [英] DialogInterface vs View OnClickListeners

查看:124
本文介绍了DialogInterface VS查看OnClickListeners的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能兼得进口 OnClickListener 。我已经进口android.view.View.OnClickListener; 但是当我想要添加进口android.content.DialogInterface.OnClickListener; 它给了我一个错误:

Why can't I have both imports for OnClickListener. I have already import android.view.View.OnClickListener; but when I want to add import android.content.DialogInterface.OnClickListener; it gives me an error:

进口android.content.DialogInterface.OnClickListener与其他import语句碰撞

这是为什么,例如,我写了 OnClickListener ,当我需要实施 DialogInterface OnClickListener (即

This is the reason why, for example, I have to write the full namespace of the OnClickListener when I need to implement a DialogInterface OnClickListener( i.e.

.setPositiveButton(R.string.ok, new android.content.DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })

任何人都可以解释我这个?我道歉,如果这是一个愚蠢的问题。

Can anyone explain me this? I apologize if this is a stupid question.

推荐答案

您不能导入两个类在同一个文件的名称相同。如果您导入两个班级的名称 X 你问一个 X ,编译器不知道哪一个类你再提到。有一个在这些情况下,一个很好的平衡。您可以替换此导入...

You can't import two classes with the same name in the same file. If you import two classes with name X and you ask for an X, the compiler doesn't know which class you're referring to. There is a nice compromise in these situations. You can replace this import...

import android.content.DialogInterface.OnClickListener;

使用这种进口...

import android.content.DialogInterface;

然后,当你需要引用特定的接口,你可以做这样的事情...

Then when you need to refer to that particular interface, you can do something like this...

.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { ... })

这工作,因为 DialogInterface OnClickListener 的嵌套静态接口的接口。这应该是对眼睛更好一点,它解决了名称冲突的问题。

This works because DialogInterface is an interface with a nested static interface named OnClickListener. This should be a little nicer on the eyes, and it solves the name collision problem.

这篇关于DialogInterface VS查看OnClickListeners的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆