gpt4 book ai didi

java - android删除 View 内的所有按钮

转载 作者:行者123 更新时间:2023-11-29 14:34:05 25 4
gpt4 key购买 nike

我有一个 LinearLayout,我在其中循环了一些 new Button 对象。我该如何正确清除该 div(例如删除所有按钮)?我已经尝试了很多次(未成功)来执行此操作,但没有任何结果。

** 编辑 **

我不确定这是否有帮助,但在 flex/AS3 中我会做类似的事情:

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

** 一点代码**

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

最佳答案

试试这个

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

假设:R.id.col2 是 LinearLayout 类型,否则使其更通用,将其类型转换为 ViewGroup。希望这有帮助!!!

关于java - android删除 View 内的所有按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6887149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com