Android:带有处理程序消息的删除按钮

Android: Delete button with handler message

我想删除我按下的按钮。由于我在 soundButtonGenerator 函数中创建了所有按钮,因此我无法使用:

View view = ((GridLayout)soundButton.getParent()).findViewById();
            ((GridLayout)soundButton.getParent()).removeView(view);

用于找到按下的按钮并将其删除。我想我可以使用处理程序消息来找到按钮并将其删除...?

public void soundButtonGenerator() {

    GridLayout layout = (GridLayout)findViewById(R.id.GL);
    layout.setColumnCount(3);
    layout.setRowCount(3);

    Point size = new Point();
    getWindowManager().getDefaultDisplay().getSize(size);
    int screenWidth = size.x;
    int screenHeight = size.y;
    int soundButtonWidth = (int)(screenWidth * 0.3);
    int soundButtonHeight = (int) (screenHeight * 0.2);

    final GradientDrawable button_press_false = new GradientDrawable();
    final GradientDrawable button_press_true = new GradientDrawable();
    button_press_false.setColor(Color.parseColor("#022864"));
    button_press_false.setCornerRadius(15);
    button_press_false.setStroke(6, Color.parseColor("#000000"));

    button_press_true.setColor(Color.parseColor("#FFFFFF"));
    button_press_true.setCornerRadius(15);
    button_press_true.setStroke(6, Color.parseColor("#000000"));

    for (int i = 0; i < soundList.size(); i++) {

        final Button soundButton = new Button(getApplicationContext());

        soundButton.setId(i);
        idList.add(soundButton.getId());
        soundButton.setText(nameList.get(i));
        soundButton.setTextColor(Color.parseColor("#FFFFFF"));
        soundButton.setWidth(soundButtonWidth);
        soundButton.setHeight(soundButtonHeight);
        soundButton.setBackgroundDrawable(button_press_false);

        layout.addView(soundButton);

        soundButton.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    sp.play(soundList.get(v.getId()), 1, 1, 0, 0, 1);
                    soundButton.setBackgroundDrawable(button_press_true);
                    soundButton.setTextColor(Color.parseColor("#000000"));

                    handle.sendMessage(soundButton); // <---------------
                    handle.postDelayed(deleteButton, 1000); // If you press the button for 1000 ms, go to the deleteButton-thread.

                    return true;
                }else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    soundButton.setBackgroundDrawable(button_press_false);
                    soundButton.setTextColor(Color.parseColor("#FFFFFF"));
                    handle.removeCallbacks(deleteButton);
                }
                return false;
            }
        });
    }
}

当您按下按钮 1000 毫秒时调用此函数:

Runnable deleteButton = new Runnable() {

    @Override
    public void run() {
       /* Obtain the sent message, find the pressed button and delete it! */
    }
};

总结:
- 按住按钮 1000 毫秒
- 找到按钮。
- 删除按钮! 4.删除它!

I want to delete a button which I pressed

使用 class 构造函数在 Runnable 的 run 方法中获取按下的按钮:

1. 通过实现 Runnable 创建内部 class :

 class DeleteButtonClass implements Runnable{
  private View clickedButton
  DeleteButtonClass(View clickedButton){
   this.clickedButton=clickedButton;
  }  
  @Override
    public void run() {
        GridLayout layout = (GridLayout)findViewById(R.id.GL);
        layout.removeView(clickedButton); //<< remove view here
    }

  }

2. 现在通过传递 v 创建 DeleteButtonClass class 的对象,这是 onTouch 方法的第一个参数或 soundButton :

DeleteButtonClass objDeleteButtonClass=new DeleteButtonClass(v);
handle.postDelayed(objDeleteButtonClass, 1000);