如何删除按钮? [英] How can I remove button?

查看:152
本文介绍了如何删除按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的按钮的名称的字符串。说它是一个名为字符串A

I have a string with the name of my button. Say it is called String A.

String A = myButtonName;

现在,如果我想要做删除的按钮:

Now, if I want to remove the button by doing:

 layout.removeView(myButtonName);

本会的工作,但是,我不能做一个字符串。

This would work, but, I can't do that on a string.

我怎样才能做到这一点对我的字符串?

How can I do it on my string?

就是这样,现在我得到一个错误,因为它是一个字符串:

Like this, right now I am getting an error, since it is a string:

 layout.removeView(A);

我怎样才能删除视图与对应于景色的字符串?

How can I remove a view with a string which corresponds to a view?

从理论上讲,我想我的字符串类型强制转换为一个ViewGroup中

Theoretically, I want to typecast my string to a ViewGroup

简化问题:

我有一个字符串。该字符串的的我的按钮的变量名。
我可以使用字符串删除的按钮?

I have a string. That string is also the variable name of my button. Can I remove the button using the string?

推荐答案

我知道了!所以,因为我只有一个字符串,我可以检查,看看是否该字符串等于对象:

I got it! So, because I have only a string, I can just check to see if that string equals the object:

if(myButtonName.equals("Blue")){

                    View myView = (View) findViewById(R.id.Blue);
                    ((ViewManager)v.getParent()).removeView(v);

                }

然后我可以手动删除对象本身。因为,如西所提到的,这是不可能删除与字符串对象的图。我检查它通过一个if语句,并更换对象之后,只是删除了看法。

Then I can manually remove the object itself. Because, as Sai mentioned, it is impossible to remove a view with a string object; I just removed the view after checking it through an if statement, and replacing the object.

您也可以使用开关:

  switch(hello) {
                case "Blue":

                    View myView = (View) findViewById(R.id.Blue);
                    ((ViewManager) v.getParent()).removeView(v);

                    break;

这篇关于如何删除按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆