2013-09-29 40 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

很高興知道!謝了哥們。 –

相關問題