onLongClick用于Android中按钮组的监听器

时间:2013-07-03 15:26:22

标签: android onlongclicklistener buttongroup

在我的Android应用程序中,我想创建一个像键盘一样工作的片段。我有一个功能,可以处理所有9个键的 onClick 。我想要知道是否还要编写一个函数来处理所有这9个键的 onLongClick

这里是布局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监听器:

        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;
        }
    });

我在一个函数中为所有 onLongClick 操作提供了所有密钥相同的ID,但它只适用于第一个密钥。无论如何在Android中定义像组按钮这样的东西???或者我必须分别为所有这些人编写OnLongClick监听器???

3 个答案:

答案 0 :(得分:6)

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

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的第一个按钮,因为它只适用于第一个按钮。

答案 1 :(得分:1)

按钮应该有不同的ID:

 <Button
    android:id="@id/testButton1"
    //... />

 <Button
    android:id="@id/testButton2"
    //... />

 <Button
    android:id="@id/testButton3"
    //... />

现在,要为所有人设置监听器,解决方案是获取数组中所有按钮的id,然后使用“for”循环为每个按钮设置监听器。

像这样:

// Store the id of buttons in an array
int[] ids={R.id.testButton1, R.id.testButton2,R.id.testButton3};

// loop through the array, find the button with respective id and set the listener    
for(int i=0; i<ids.length; i++){
  Button button = (Button) findViewById(ids[i]);
  button.setOnLongClickListener(longClickListener);
}

View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
        // .....
        return true;
    }
};

答案 2 :(得分:0)

如果您的按钮位于LinearLayout,RelativeLayour或其他按钮中。你有很多按钮。

    View.OnLongClickListener listener = new View.OnLongClickListener() {
        public boolean onLongClick(View v) {
            Log.d("ID", ""+v.getId());
            switch (v.getId())
            {
            case R.id.button1:
                //do something for button1
                break;
            case R.id.button2:
                //do something for button2
                break;
            ...
            }
            return true;
        }
    };


    LinearLayout layout=(LinearLayout)findViewById(R.id.IdOfLinearLayout);
    for(int i=0;i<layout.getChildCount();i++){
          layout.getChildAt(i).setOnLongClickListener(listener);
      }

xml文件,例如......

<LinearLayout 
    android:id=@+id/IdOfLinearLayout"....>
    <Button
       android:id="@+id/button1"
       android:layout_width=...
       android:layout_height=...
    .../>
    <Button
       android:id="@+id/button2"
       ... />
     ....
    <Button
       android:id="@+id/buttonN"
       .../>
</LinearLayout>
相关问题