How can I remove a Button in its own onClick method?
I want to remove a Button in his own onClick method after it's clicked. I tried it with the normal way: layout.removeView(save); But the button will not be removed and I get no error. If I want to add the Button I get an error because 开发者_开发问答the button already excists.
I think it isn't working because I trie to remove the button during his OnClickHandler is active. So my Question is how can I remove the button after he is clicked?
Here's the complete, fully tested solution:
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
view.setVisibility(View.GONE);
}
});
You can also completely remove the view from the layout like this (also tested):
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
ViewGroup parentView = (ViewGroup) view.getParent();
parentView.removeView(view);
}
});
Try to set its state with button.setVisibility(Visibility.GONE)
How about just hide it? e.g. in your button onclick handler you can do something like:
button.setVisibility(View.GONE);
精彩评论