Android中的按钮组的onLongClick侦听器 [英] onLongClick listener for group of buttons in Android

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

问题描述

在我的Android应用程序中,我想创建一个类似于键盘的片段。我有一个功能,它可以对所有9个键进行 onClick 。我想知道是否也只编写了一个函数来处理所有这9个键的 onLongClick

In my Android application, I want to create a fragment that works like a keypad. I have one function which handle onClick for all 9 keys. I want to know is there anyway to write just one function to handle onLongClick for all these 9 keys too.

这是layout xml: / p>

here is layout xml :

   <Button
    android:id="@id/testButton"
    android:layout_width="70dp"
    android:layout_height="55dp"
    android:layout_margin="2dp"
    android:background="@drawable/keypad_round_button"
    android:text="1"
    android:textColor="@color/black_1" 
    android:onClick="keypadSetNote"
    android:longClickable="true"/>
<Button
    android:id="@id/testButton"
    android:layout_width="70dp"
    android:layout_height="55dp"
    android:layout_margin="2dp"
    android:background="@drawable/keypad_round_button"
    android:text="2"
    android:longClickable="true"
    android:textColor="@color/black_1"
    android:onClick="keypadSetNote"
     />

这里是OnlongClick侦听器:

Here is OnlongClick listener :

        Button button = (Button) findViewById(R.id.testButton);
    button.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            Button clickedButton = (Button) v;
            String buttonText = clickedButton.getText().toString();
            Log.v(TAG, "button long pressed --> " + buttonText);
            return true;
        }
    });

我为所有键赋予了相同的ID,以在一个函数中处理所有 onLongClick 操作,但仅适用于第一把钥匙。无论如何,有没有在Android中定义类似群组按钮的功能?还是我必须分别为它们全部编写OnLongClick侦听器??

I gave all keys same ID to handle all onLongClick actions in one function, but it just work for the first key. Is there anyway to define something like group button in Android ??? Or I have to write OnLongClick listener for all of them separately ???

推荐答案

只需创建一个命名函数而不是一个匿名函数即可。 :

Just create a named function instead of an anonymous one:

View.OnLongClickListener listener = new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
        Button clickedButton = (Button) v;
        String buttonText = clickedButton.getText().toString();
        Log.v(TAG, "button long pressed --> " + buttonText);
        return true;
    }
};

button1.setOnLongClickListener(listener);
button2.setOnLongClickListener(listener);
button3.setOnLongClickListener(listener);

而且,尽管您没有问过,但我们必须警告您:正如@AndyRes所说,您的按钮必须具有不同的ID。具有相同的ID并不意味着按ID获取按钮将返回所有按钮,只会返回具有该ID的第一个按钮,这是因为它仅与第一个按钮一起使用。

And, altough you don't asked it, we must warn you: As @AndyRes says, your buttons must have differents ids. Having the same ID doesn't mean that getting the button by id will return all buttons, only will return the first button with that ID, that's because it only works with the first button.

这篇关于Android中的按钮组的onLongClick侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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