Android 动态删除按钮

标签 android android-layout

我有一个按钮,当我按下它时,我想将其移除(而不是使其不可见)。我读到我可以使用 layout.removeView(mybutton) 来做到这一点,但布局是什么?我怎样才能在我的 Activity 中得到它

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;
    }
}

最佳答案

参见 link

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

关于Android 动态删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169360/

相关文章:

android - 如何让线性布局填充Android中的父页面

java - 整个屏幕的OnTouchListener

android - 在 Android 11 的后台访问麦克风/摄像头

android - 如何使用 JSON 对象将编码的 Base64 图像字符串发送到服务器

android - Google Play 服务修订版 24

android - android 布局上的简单 trans 动画第二次不起作用

android - 在安卓中;是应用 OnClickListener 更好还是使用 android :onClick?

android - Xamarin Forms(Android) - 升级和降级订阅

android - 从android中的纬度和经度获取城市名称提供服务不可用Android 4.0.3?

android - gridlayout 2列中心水平