2016-01-13 46 views
-1

我想刪除我按下的按鈕。自從我創造我的soundButtonGenerator功能的所有按鈕無法使用:Android:帶處理程序消息的刪除按鈕

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

+0

究竟什麼是你的錯誤或問題? – Knossos

+0

@Knossos:我想刪除一個我按下的按鈕,使用問題底部的run方法。 – Wickerman

+0

...然後做。你似乎已經找到了代碼。什麼是實際問題? – Knossos

回答

0

我想刪除一個按鈕,我在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); 
+0

刪除按下的按鈕看起來像一個很好的解決方案,但removeView無法解析? – Wickerman

+0

@Wickerman:看我的編輯答案 –

+0

它的工作原理!非常感謝你! :) – Wickerman