Android的:如何处理按钮点击如何处理、按钮、Android

2023-09-03 22:16:50 作者:借酒劲吻你

具有非Java和非Android领域坚实的经验,我学的Andr​​oid。

Having a solid experience in non-Java and non-Android area, I'm learning Android.

我有很多的困惑与不同领域,其中之一就是如何处理按钮点击。有这样做至少有4路(!),他们正在这里简要列出:http://www.remwebdevelopment.com/dev/a69/Different-Ways-To-Handle-Clicks.html

I have a lot of confusion with different areas, one of them is how to handle button clicks. There are at least 4 way of doing that (!!!), they are briefly listed here: http://www.remwebdevelopment.com/dev/a69/Different-Ways-To-Handle-Clicks.html

有一致性的目的,我将一一列举:

for consistency purpose I will list them:

有活动中的View.OnClickListener'类的成员,并将其分配给一个实例,将处理的onClick逻辑的的onCreate活动的方法。

Have a member of the 'View.OnClickListener' class in the activity and assign it to an instance that will handle 'onClick' logic in the 'onCreate' activity method.

在的onCreate活动的方法创建onClickListener并将其分配给使用setOnClickListener按钮

Create 'onClickListener' in the 'onCreate' activity method and assign it to the button using setOnClickListener

实现onClickListener的活动本身并分配'这个'作为一个侦听器的按钮。因为,如果活动有几个按钮的情况下,按钮ID应分析执行的onClick处理程序正确的按钮

Implement 'onClickListener' in activity itself and assign 'this' as a listener for the button. For the case if activity has few buttons, button id should be analyzed to execute 'onClick' handler for the proper button

有public方法上实现的onClick逻辑的活动,并在活动XML声明分配到按钮

Have public method on the activity that implements 'onClick' logic and assign it to the button in the activity xml declaration

问题1:

这是所有方法,有没有其他的选择吗? (我不需要任何其他的,只是好奇)

Are those all methods, is there any other option? (I don't need any other, just curious)

对于我来说,最直观的方式将最新之一:它需要最少量的code被键入,是最可读的(至少对我来说)

For me, the most intuitive way would be the latest one: it requires the least amount of code to be typed and is the most readable (at least for me).

不过,我不认为这种方法得到广泛应用。有什么缺点使用它?

Though, I don't see this approach used widely. What are cons for using it?

问题2:

有什么优点/缺点的这些方法?请分享你的任何经验或一个很好的链接。

What are pros/cons for each of these methods? Please share either your experience or a good link.

任何意见是值得欢迎的!

Any feedback is welcome!

P.S。我一直在努力,谷歌和找到了这个话题,但我发现的唯一的东西是介绍如何做到这一点,不为什么是好还是坏。

P.S. I've tried to Google and find something for this topic, but the only things I've found are description "how" to do that, not why is it good or bad.

推荐答案

问题1: 不幸的是,在其中你,你说的是最直观的是至少在Android中使用。据我了解,你应该分开你的UI(XML)和计算功能(Java类文件)。这也使得更容易调试。它实际上是一个更容易阅读的方式,并认为关于Android海事组织。

Question 1: Unfortunately the one in which you you say is most intuitive is the least used in Android. As I understand, you should separate your UI (XML) and computational functionality (Java Class Files). It also makes for easier debugging. It is actually a lot easier to read this way and think about Android imo.

问题2: 相信主要使用的两个是#2和#3。我将使用一个按钮clickButton作为一个例子。

Question 2: I believe the two mainly used are #2 and #3. I will use a Button clickButton as an example.

在一个匿名类的形式。

Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ***Do what you want with the click here***
            }
        });

这是我最喜欢的,因为它有onClick的方法旁边到按钮变量设置与findViewById。它看起来很整洁,一切与此clickButton按钮来查看处理就设在这里。

This is my favorite as it has the onClick method right next to where the button variable was set with the findViewById. It seems very neat and tidy that everything that deals with this clickButton Button View is located here.

一个骗子,我的同事的意见,就是想象你有需要的onclick听众很多意见。你可以看到你的onCreate会得到很长的长度。这样,为什么他喜欢用:

A con that my coworker comments, is that imagine you have many views that need onclick listener. You can see that your onCreate will get very long in length. So that why he likes to use:

说你有,5 clickButtons:

Say you have, 5 clickButtons:

请确保您的活动/片段实现OnClickListener

Make sure your Activity/Fragment implement OnClickListener

// in OnCreate

Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
mClickButton2.setOnClickListener(this);
Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
mClickButton3.setOnClickListener(this);
Button mClickButton4 = (Button)findViewById(R.id.clickButton4);
mClickButton4.setOnClickListener(this);
Button mClickButton5 = (Button)findViewById(R.id.clickButton5);
mClickButton5.setOnClickListener(this);


// somewhere else in your code

public void onClick(View v) {
    switch (v.getId()) {
        case  R.id.clickButton1: {
            // do something for button 1 click
            break;
        }

        case R.id.clickButton2: {
            // do something for button 2 click
            break;
        }

        //.... etc
    }
}

此方式,我的同事解释说是在他眼中更整洁,因为所有的onClick计算是在一个地方处理,而不是拥挤的onCreate方法。但不足之处我看到的是,即:

This way as my coworker explains is neater in his eyes, as all the onClick computation is handled in one place and not crowding the onCreate method. But the downside I see is, that the:

的意见自己, ,并且可能位于的onCreate所使用的方法的onClick任何其他对象将不得不被制成字段

让我知道,如果你想了解更多信息。我没有回答你的问题,完全是因为它是pretty的长期问题。如果我发现一些网站,我会扩大我的答案,现在我只是给了一些经验。

Let me know if you would like more information. I didn't answer your question fully because it is a pretty long question. And if I find some sites I will expand my answer, right now I'm just giving some experience.