简体   繁体   English

Android删除视图内的所有按钮

[英]android remove all buttons inside a view

I have a LinearLayout which I've looped a number of new Button objects into. 我有一个LinearLayout ,我已将许多new Button对象循环到其中。 How do I go about clearing that div correctly (eg removing all the buttons)? 如何正确清除该div(例如,删除所有按钮)? I've tried a number of times (unsuccessfully) to do this, but have nothing to show for it. 我已经尝试过多次(失败了),但是没有任何显示。

** edit ** **编辑**

I'm not sure if this helps, but in flex/AS3 I would do something like: 我不确定这是否有帮助,但是在flex / AS3中,我会做类似的事情:

while(myView.numChildren) myView.removeChildAt(0);

** a little code ** **一点代码**

View col1 = findViewById(R.id.col1);
for(final Map.Entry<String,HashMap<String,String>> entry : _nav.entrySet()) {
    Button item = new Button(this);
    item.setText(entry.getKey());
    item.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            openCol2(entry);
        }
    });
    ((LinearLayout) col1).addView(item);
}

private final void openCol2(Map.Entry<String,HashMap<String,String>> entry) {
    View col2 = findViewById(R.id.col2);
    // here is where I want to clean out col2. Right before I add more buttons.

    for(int i = 0; i < _nav.size(); ++i) {
        Button item = new Button(this);
        //item.setText(entry.getKey());
        ((LinearLayout) col2).addView(item);
    }
}

Try this 尝试这个

LinearLayout col2 = (LinearLayout)findViewById(R.id.col2);
col2.removeAllViews();

Assumption: R.id.col2 is of LinearLayout type else to make it more generic typecast it to ViewGroup. 假设:R.id.col2具有LinearLayout类型,否则将使其更通用地类型转换为ViewGroup。 Hope this help!!! 希望这个帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM