Android动态删除按钮

Tot*_*tti 9 android android-layout

我有一个按钮,当我按下它时,我想删除它(不要让它看不见).我读到我可以使用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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Dhe*_*ngh 17

看到链接

ViewGroup layout = (ViewGroup) button.getParent();
if(null!=layout) //for safety only  as you are doing onClick
  layout.removeView(button);
Run Code Online (Sandbox Code Playgroud)


Par*_*ani 13

我有一个按钮,当我按下它,我想删除它(不要让它隐形)

=>你做了如下:

 showQuestion.setVisibility(View.INVISIBLE);
Run Code Online (Sandbox Code Playgroud)

试试:

 showQuestion.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

仅供参考,不可见只是隐藏视图但实际存在于那里,GONE也隐藏了物理存在.