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

时间:2016-01-13 14:38:42

标签: android button handler message

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

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

查找按下的按钮并将其删除。我想我可以使用handler-messages来找到按钮并将其删除......?

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.删除它!

1 个答案:

答案 0 :(得分:0)

  

我想删除我按下的按钮

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

1。通过实施Runnable创建内部类:

 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方法的第一个参数onTouch方法或soundButton来创建DeleteButtonClass类的对象:

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