Android:如何处理按钮点击 [英] Android: how to handle button click

查看:29
本文介绍了Android:如何处理按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在非 Java 和非 Android 领域有丰富的经验,我正在学习 Android.

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

我对不同的领域有很多困惑,其中之一是如何处理按钮点击.至少有 4 种方法 (!!!),它们被简要列出 这里

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

为了保持一致性,我将列出它们:

for consistency purpose I will list them:

  1. 在活动中有一个 View.OnClickListener 类的成员,并将其分配给一个实例,该实例将处理 onCreate<中的 onClick 逻辑/code> 活动方法.

  1. 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"并将this"指定为按钮的侦听器.如果 Activity 的按钮很少,则应分析按钮 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

对实现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)

对我来说,最直观的方法是最新的:它需要输入的代码最少,而且可读性最好(至少对我来说).

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.

欢迎任何反馈!

附言我试过用谷歌搜索这个主题的东西,但我发现的唯一的东西是描述如何"做到这一点,而不是为什么好或坏.

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 imo实际上容易得多.

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.我将使用 Button 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***
            }
        });

这是我最喜欢的,因为它在使用 findViewById 设置按钮变量的位置旁边有 onClick 方法.处理这个 clickButton Button View 的所有东西都位于这里,看起来非常整洁.

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 个点击按钮:

Say you have, 5 clickButtons:

确保您的 Activity/Fragment 实现 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:

  1. 查看自己,
  2. 以及可能位于 onCreate 中由 onClick 方法使用的任何其他对象都必须成为一个字段.

如果您想了解更多信息,请告诉我.我没有完全回答你的问题,因为这是一个很长的问题.如果我找到一些网站,我会扩大我的答案,现在我只是提供一些经验.

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.

这篇关于Android:如何处理按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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