In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to bind UI through annotations in android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this android article on how to bind UI through annotations. Let's take a look at it.
The first type: customize an annotation class: ViewInject
/ * *
* Custom annotations to bind the Id of UI
* 1.@Target indicates the type of annotation required:
* a.ElementType.TYPE:
* classes, interfaces, enumerated types
* b.ElementType.FIELD:
* attribute
* c.ElementType.METHOD:
* method
* d.ElementType.PARAMETER:
* parameters
* e.ElementType.CONSTRUCTOR:
* Constructor
* f.ElementType.LOCAL_VARIABLE:
* Local variables
* g.ElementType.ANNOTATION_TYPE:
* Annotation type
* h.ElementType.PACKAGE:
* package
*
* 2.@Retention indicates the scope of comment retention:
* a.RetentionPolicy.SOURCE:
* comments are only valid in the source code
* b.RetentionPolicy.CLASS:
* comments are valid in source code and class, which is the default policy
* c.RetentionPolicy.RUNTIME:
* comments are valid in source code, class files, and runtime
*
* @ author Yanbao_Wu
, /
@ Target (ElementType.FIELD)
@ Retention (RetentionPolicy.RUNTIME)
Public @ interface ViewInject {
Int value ()
}
Type 2: create our own activity base class: BaseActivity
/ * *
* Activity base class, which supports binding UI through annotations
*
* @ author Yanbao_Wu
* @ date: 2014-11-21
, /
Public abstract class BaseActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (initLayoutId ())
InjectViews ()
}
/ * *
* bind id to annotated View by reflection
* @ author Yanbao_Wu
, /
Protected void injectViews () {
Field [] fields = this.getClass () .getDeclaredFields
For (Field field: fields) {
If (field.isAnnotationPresent (ViewInject.class)) {
ViewInject inject = field.getAnnotation (ViewInject.class)
Int id = inject.value ()
If (id > 0) {
Field.setAccessible (true)
Try {
Field.set (this, this.findViewById (id))
} catch (IllegalArgumentException e) {
E.printStackTrace ()
} catch (IllegalAccessException e) {
E.printStackTrace ()
}
}
}
}
}
Public abstract int initLayoutId ()
}
The ape finally came back and was happy with the leader:
Public class MainActivity extends BaseActivity {
@ ViewInject (R.id.test_anotation)
Private TextView anotationText
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
/ / successful binding of id,text in the way of annotation will be set, otherwise a null pointer exception will be thrown
AnotationText.setText ("Test Anotation")
}
@ Override
Public int initLayoutId () {
Return R.layout.activity_main
}
}
This is the end of the article on "how to bind UI through annotations in android". Thank you for reading! I believe you all have a certain understanding of "how to bind UI through annotations in android". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.