开发者

Android typed controls instead of findViewById

开发者 https://www.devze.com 2022-12-14 14:56 出处:网络
Is it somehow possible that instead of: 开发者_如何学编程 Button btnNextWord = (Button) this.findViewById(R.id.btnNextWord);

Is it somehow possible that instead of:

开发者_如何学编程 Button btnNextWord = (Button) this.findViewById(R.id.btnNextWord);

Eclipse automatically generates for me something like:

Button btnNextWord = this.btnNextWord;

or

Button btnNextWord = R.id.getBtnNextWord(this);

??


No, because what you're doing in requesting a reference to a child element that has a certain name and Eclipse isn't actually loading in the layout xml so it can't know what is in it.


I know this is a very late response but it might help someone who read.

You can use the android annotations library for doing something similar to what you want.

As you can see on their page, you can write a field in the code like this:

@ViewById
ListView bookmarkList;

The library will findById an item with id R.id.bookmarkList and cast it to ListView.

Or you can use the annotation like this

@ViewById(R.id.myTextView)
TextView someName;

if you want to give a better name to the field.

DISCLAIMER: I never used the library so I'm not sure whether you can use just that annotation or you're bound to use their whole set of annotations.

0

精彩评论

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