9

我有一个按钮,当我按下它时,我想删除它(而不是让它不可见)。我读到我可以这样做,layout.removeView(mybutton)但布局是什么?以及如何在我的活动中获得它

Button showQuestion;
private void initialize() {
    showQuestion = (Button) findViewById(R.id.bAnswerQuestionShowQuestion);
}
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bAnswerQuestionShowQuestion:
                showQuestion.setVisibility(View.INVISIBLE);
                //Here i want to delete the button
                question.setVisibility(View.VISIBLE);
                theAnswer.setVisibility(View.VISIBLE);
                answerQuestion.setVisibility(View.VISIBLE);
                showChoices.setVisibility(View.VISIBLE);
                showHint.setVisibility(View.VISIBLE);
            break;
    }
}
4

4 回答 4

18

链接

ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only  as you are doing onClick
  layout.removeView(button);
于 2012-06-23T12:07:44.190 回答
14

我有一个按钮,当我按下它时,我想删除它(不要让它不可见)

=> 你做了如下:

 showQuestion.setVisibility(View.INVISIBLE);

尝试:

 showQuestion.setVisibility(View.GONE);

仅供参考,INVISIBLE 只是隐藏视图但实际存在于那里,并且 GONE 隐藏以及物理上移除存在。

于 2012-06-23T12:13:21.007 回答
4

您可以使用

      button.setVisibility(View.GONE);
于 2012-06-23T12:10:36.403 回答
1

Layout 是 Button 的父 Layout,通常是 RelativeLayout 或 LinearLayout。

您可以按以下方式获取它:

ViewParent layout = button.getParent();
于 2012-06-23T12:09:23.910 回答