有关Java中的点击事件处理的疑问 [英] Doubts regarding click event handling in Java
问题描述
大家好,
样本1:
Hi All,
Sample 1 :
Button mButton = new Button();
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mText.showDropDown();
}
});
样本2:
Sample 2 :
Button mButton = new Button()
MyClick myClick = new MyClick();
mButton.setOnClickListener(myClick);
class MyClick implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
两者之间有什么区别?
What is the difference between both?
推荐答案
这是两个都能实现的实现.
示例1 是一劳永逸的.快速,没什么可考虑的.但是也不擅长维护或需要更改GUI时.您将始终在演示文稿部分中执行操作-不太好.
示例2 是我的推荐.每个GUI部分旁边都有一个Worker类(使其成为自己文件中的真实类,而不是此处的匿名类). Worker是事件监听器,还负责提供GUI需要的所有数据. 优点:您可以弄乱GUI,而无需触摸它后面的数据.您可以覆盖GUI而不会丢失功能部件.结合大量的OOP,您将拥有一个不错的GUI.
These are 2 implementation that both work.
Sample 1 is fire-and-forget. Fast, not much to think about. But also not good at maintenance or when the GUI needs to change. You will always have the action in the presentation portion - not really good.
Sample 2 is my recommendation. Each GUI portion get''s a Worker-class beside itself (make it a real class at own file, not like here an anonymous class). The Worker is the Event-Listener and is also responsible to deliver all data the GUI needs. Benefit: you can mess up the GUI without touching the Data behind it. You can override a GUI without loosing functional parts. Combined with a good portion of OOP you''ll have a great GUI.
匿名类-嗯,有时有人使用它.就像一个简单的高级数据类型为应用程序使用的每个对象保留一些附加值或保留一个ID一样:
Anonymous classes - well, sometimes one uses that. Like for a simple advanced datatype to hold some additional value or to hold an ID for each object the application is working with:
public class EMailStore{
private List<customemail> emails = new List<customemails>();
/* more Fancye code */
// anonymous classes are often written at the end of the class
// to make sure the developer figures out that this is another class (I guess)
public class CustomMail{// anonymous Type for an Email with a read status
public EMail email;
public boolean isRead=false;
CustomMail(Email email){
this.email = email;
}
}
}
</customemails></customemail>
因此,在这里我可以处理一些电子邮件,并在其中添加了读取状态,即我从中收到邮件的电子邮件既没有,也不需要,不想要任何东西.
不同的故事是通过使用另一个类/接口中的类来构造一些信息.我将这样的值用于GUI中的ID之类的固定值:
So here I can work with some emails, to which I add a read status, that the one I got the mail from doesn''t have or want or whatever.
Different story is to structure some information by using classes inside another Class/Interface. I use such for fixed values like ID''s in my GUIs:
public interface IMyGui{
public class Def{
public static final String
NAME = "",
STREET = "",
ZIPCODE = "";
}
}
public class MyGui implements IMyGui{
/* some code */
CustomText oText = new CustomText(Def.Name); // creation of cutomized Textbox with String param as ID.
}
然后,我可以一次又一次地引用相同的ID(例如在SelectionEvent的操作中,我只需检查ID以查看需要执行的操作即可).
I can then refer to that same ID again and again (like in the action for the SelectionEvent, where I simply check the ID to see which action I need to kick off).
这篇关于有关Java中的点击事件处理的疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!