我想刪除我按下的按鈕。自從我創造我的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.刪除它!
究竟什麼是你的錯誤或問題? – Knossos
@Knossos:我想刪除一個我按下的按鈕,使用問題底部的run方法。 – Wickerman
...然後做。你似乎已經找到了代碼。什麼是實際問題? – Knossos