Android查看是否按钮';s文本包含字符串
我的应用程序中有一长串单选按钮Android查看是否按钮';s文本包含字符串,android,Android,我的应用程序中有一长串单选按钮 如何删除文本中不包含字符串“test”的所有按钮?如果将它们放在类似列表的列表中,则非常简单 List<RadioButton> testButtons = new ArrayList<RadioButton>(); for (RadioButton button: radioButtonList) { if (button.getText().toString().contains("test")) { test
如何删除文本中不包含字符串“test”的所有按钮?如果将它们放在类似列表的列表中,则非常简单
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);
}
List testButtons=new ArrayList();
用于(RadioButton按钮:RadioButton列表){
if(button.getText().toString()包含(“测试”)){
添加(按钮);
}
}
//假设它们都具有相同的父视图
视图parentView=findviewbyd(R.id.parentView);
用于(单选按钮测试按钮:测试按钮){
parentView.removeView(按钮)
//或者正如Evan B所建议的那样,这更简单(尽管从一般意义上来说,它并没有被从视图中“移除”)
testButton.setVisibility(消失);
}
如果将它们放在一个类似列表的列表中,则非常简单
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);
}
List testButtons=new ArrayList();
用于(RadioButton按钮:RadioButton列表){
if(button.getText().toString()包含(“测试”)){
添加(按钮);
}
}
//假设它们都具有相同的父视图
视图parentView=findviewbyd(R.id.parentView);
用于(单选按钮测试按钮:测试按钮){
parentView.removeView(按钮)
//或者正如Evan B所建议的那样,这更简单(尽管从一般意义上来说,它并没有被从视图中“移除”)
testButton.setVisibility(消失);
}
一个按钮的示例:
Button buttonOne = (Button) findViewById(R.id.buttonOne);
removeButtons();
public void removeButtons() {
if (buttonOne.getText().toString() != "test") {
buttonOne.setVisibility(GONE);
}
}
如果有阵列,请将其打开。一个按钮的示例:
Button buttonOne = (Button) findViewById(R.id.buttonOne);
removeButtons();
public void removeButtons() {
if (buttonOne.getText().toString() != "test") {
buttonOne.setVisibility(GONE);
}
}
如果您有阵列,请将其打开。您可以自动执行以下操作:
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 vg=(ViewGroup)findviewbyd(R.id.your_布局);
int-iter=0;
而(iter您可以自动执行此操作:
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 vg=(ViewGroup)findviewbyd(R.id.your_布局);
int-iter=0;
while(iterHow)您是如何创建这些按钮的?动态还是通过xml布局?@jeraldov我在我的xml布局文件中生成它们?如何创建这些按钮?动态还是通过xml布局?@jeraldov我在我的xml布局文件中生成它们您必须使用.equals()
用于字符串比较——您不能使用=
或!=
。很高兴知道!谢谢,伙计。您必须使用.equals()
用于字符串比较——你不能使用=
或!=
。很高兴知道!谢谢你的回答,伙计。谢谢你的回答,但我最终使用了Eudhan的答案。谢谢你的回答,但我最终使用了Eudhan的答案。谢谢你的回答,经过一点调整后,它工作得很好!谢谢你的回答,af稍微调整一下,效果非常好!