2013-09-29 38 views

回答

1

你可以自动完成:

ViewGroup vg= (ViewGroup) findViewById(R.id.your_layout); 

int iter=0; 
while(iter<vg.getChildCount()){ 
boolean found=false; 
View rb=vg.getChildAt(iter); 

if(rb instanceof RadioButton){ 
    if(rb.getText().toString().contains(my_string)){//found a pattern 
    vg.removeView(rb);//remove RadioButton 
    found=true; 
    } 
} 
if(!found) ++iter;//iterate on the views of the group if the tested view is not a RadioButton; else continue to remove 

} 

上面的代码不与其他的ViewGroup内viewgroups处理(例如里面的LinearLayout另一个)。在调用removeView之后,我没有测试迭代器的代码和viewgroup的状态;你可以在控制台中检查并告诉我们。

+0

感谢这个答案,经过一些调整,它完美的工作! – superuser

1

如果你把它们放在一起,像列表一样,这很简单。

为一个按钮
List<RadioButton> testButtons = new ArrayList<RadioButton>(); 
for (RadioButton button: radioButtonList) { 
    if (button.getText().toString().contains("test")) { 
     testButtons.add(button); 
    } 
} 

// assuming that they all have the same parent view 
View parentView = findViewById(R.id.parentView); 
for (RadioButton testButton: testButtons) { 
    parentView.removeView(button) 
    // or as Evan B suggest, which is even simpler (though then it is not 'removed' from the view in the litteral sense 
    testButton.setVisibility(GONE); 
} 
+0

谢谢,更新回答 – cYrixmorten

+0

谢谢你的回答,但我最终使用了Eudhan的答案版本。 – superuser

1

实施例:

Button buttonOne = (Button) findViewById(R.id.buttonOne); 

removeButtons(); 


public void removeButtons() { 
    if (buttonOne.getText().toString() != "test") { 
     buttonOne.setVisibility(GONE); 
    } 
} 

开关起来,如果你有一个数组。

+2

您必须使用'.equals()'进行字符串比较 - 您不能使用'=='或'!='。 – kcoppock

+0

很高兴知道!谢了哥们。 –

相关问题