开发者

Pass Control By Name To findViewById

If you got:

Button btn = (Button) findViewById(R.id.widget28);

where "widget28" is the name of your button, how can you pass this into the findViewById as a String? or get it from R class as a string?

I want to get the Button instance by开发者_高级运维 string and not by the hard code R.id.widget28 reference.

thanks


Note that findViewById expects an int, not a string, so not sure what you're trying to achieve here.

But the general way to get a string from its resource id is getString(R.string.theId). See the Context API docs.

I don't believe even with reflection that you can get the variable's name, widget28, if you actually need it for some reason. You could look into using the button's android:tag element to store it in the XML file, you'd then retrieve it in code by using yourButton.getTag(), but I'd look into designing it differently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜