Android 动态删除按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11169360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:12:56  来源:igfitidea点击:

Android remove button dynamically

androidandroid-layout

提问by Totti

I have a button and when I press it, i want to remove it (not make it invisible). I read that I can do that using layout.removeView(mybutton)but what is the layout ? and how can I get it in my activity

我有一个按钮,当我按下它时,我想将其移除(而不是使其不可见)。我读到我可以使用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;
    }
}

回答by Dheeresh Singh

see link

链接

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

回答by Paresh Mayani

i have a button and when i press it , i want to remove it (not make it invisible)

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

=> You did as below:

=> 你做了如下:

 showQuestion.setVisibility(View.INVISIBLE);

Try with:

尝试:

 showQuestion.setVisibility(View.GONE);

FYI, INVISIBLE just hide the view but physically present there and GONE hide as well remove the presence physically as well.

仅供参考,INVISIBLE 只是隐藏视图,但实际存在于那里,而 GONE 隐藏也会从物理上移除存在。

回答by Yogesh Somani

You can use

您可以使用

      button.setVisibility(View.GONE);

回答by Raghav Sood

Layout is the parent Layout of your Button, usually a RelativeLayout or LinearLayout.

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

You can get it as follows:

您可以通过以下方式获取它:

ViewParent layout = button.getParent();