Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 注解在android中的使用

注解在android中的使用

編輯:關於Android編程

注解在android程序中的使用

何為注解:

在Java當中,注解又叫做“元數據”,它為我們在源代碼中添加信息提供了一種形式化的方法,讓我們能在以後的某個時間方便的使用這些數據。更確切的說,注解在一定的程度上將數據與元代碼進行了綁定,並不是將數據保存在外部文件中。當然,這裡只介紹Android開發相關的技術,至於注解我會在Java學習總結篇中進行詳細的研究和總結。那麼,接下來我們一個實例來介紹注解在android開發中的巧妙應用。

實例描述:

在這裡,我們使用注解替換掉android中初始化資源對象的findViewById(...)方式,具體如下代碼所示:

我的項目結構:

\

注解部分:

ContentView.java:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+LyoqPC9wPgo8cD4gKiBpbmplY3QgY29udGVudHZpZXc8L3A+CjxwPiAqLzwvcD4KPHA+QFRhcmdldChFbGVtZW50VHlwZS5UWVBFKTwvcD4KPHA+QFJldGVudGlvbihSZXRlbnRpb25Qb2xpY3kuUlVOVElNRSk8L3A+CjxwPnB1YmxpYyBAaW50ZXJmYWNlIENvbnRlbnRWaWV3IHs8L3A+CjxwPmludCB2YWx1ZSgpIGRlZmF1bHQgMDs8L3A+CjxwPn08L3A+CjxwPiA8L3A+CjxwPkluamVjdFZpZXcuamF2YTo8L3A+CjxwPi8qKjwvcD4KPHA+ICogaW5qZWN0IHZpZXcgb2YgY29udGVudHZpZXc8L3A+CjxwPiAqLzwvcD4KPHA+QFRhcmdldChFbGVtZW50VHlwZS5GSUVMRCk8L3A+CjxwPkBSZXRlbnRpb24oUmV0ZW50aW9uUG9saWN5LlJVTlRJTUUpPC9wPgo8cD5wdWJsaWMgQGludGVyZmFjZSBWaWV3SW5qZWN0IHs8L3A+CjxwPmludCB2YWx1ZSgpOzwvcD4KPHA+aW50IHBhcmVudFJlc0lkKCkgZGVmYXVsdCAwOzwvcD4KPHA+f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dDs8L3A+CjxwPn08L3A+CjxwPiA8L3A+CjxwPkBPdmVycmlkZTwvcD4KPHA+cHVibGljIFN0cmluZyB0b1N0cmluZygpIHs8L3A+CjxwPnJldHVybiA="ViewInjectObj [parentResId=" + parentResId + ", value=" + value

+ "]";

}

ViewSelector.java:

/**

* find view id resources

*/

public class ViewSelector {

private Activity currActivity;

private View currView;

public ViewSelector(Activity activity) {

currActivity = activity;

}

public ViewSelector(View view) {

currView = view;

}

public View findViewById(int id) {

return null == currView ? currActivity.findViewById(id) : currView.findViewById(id);

}

public View findViewByInfo(ViewInjectObj info) {

return findViewById((Integer) info.value, info.parentResId);

}

public View findViewById(int id,int pId) {

View pView = null;

if(0 < pId) {

pView = findViewById(pId);

}

View currView = null;

if(null != pView) {

currView = pView.findViewById(id);

} else {

currView = findViewById(id);

}

return currView;

}

}

映射方法部分:

InjectConfig.java:

/**

* inject view methods

*/

public class InjectConfig {

static final String TAG = "InjectConfig";

public InjectConfig() {

}

// inject contentview

public void injectContentView(Object obj,Class objClass) {

ContentView contentView = objClass.getAnnotation(ContentView.class);

if(null != contentView) {

try {

Method method = objClass.getMethod("setContentView", int.class);

method.invoke(obj, contentView.value());

} catch(Throwable tr) {

Log.e(TAG, tr.getMessage(), tr);

}

}

}

// inject view

public void injectView(Object obj,ViewSelector viewSelector,Class objClass) {

Field[] fields = objClass.getDeclaredFields();

if(null != fields && 0 < fields.length) {

for(Field field : fields) {

ViewInject viewInject = field.getAnnotation(ViewInject.class);

if(null != viewInject) {

try {

View view = viewSelector.findViewById(viewInject.value(), viewInject.parentResId());

if(null != view) {

field.setAccessible(true);

field.set(obj, view);

}

} catch(Throwable tr) {

Log.e(TAG, tr.getMessage(), tr);

}

}

}

}

}

}

調用工具部分:

ViewUtilSvc.java:

public class ViewUtilSvc {

static InjectConfig config = null;

public static void inject(Activity activity) {

inject(activity,new ViewSelector(activity));

}

public static void inject(View view) {

inject(view,new ViewSelector(view));

}

public static void inject(Object obj, View view) {

inject(obj, new ViewSelector(view));

}

static void inject(Object obj,ViewSelector viewSelector) {

if(null == config) {

config = new InjectConfig();

}

Class objClass = obj.getClass();

config.injectContentView(obj, objClass);

config.injectView(obj, viewSelector, objClass);

}

}

前台顯示部分:

MainActivity.java:

@ContentView(R.layout.activity_main)

public class MainActivity extends FragmentActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// setContentView(R.layout.activity_main);

ViewUtilSvc.inject(this);

if (savedInstanceState == null) {

getSupportFragmentManager()

.beginTransaction()

.add(R.id.container, new PlaceholderFragment())

.commit();

}

}

public static class PlaceholderFragment extends Fragment {

@ViewInject(R.id.tvHelloWorld)

TextView tvHelloWorld;

public PlaceholderFragment() {

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container,false);

ViewUtilSvc.inject(this,rootView);

String hello = getResources().getString(R.string.text_helloworld_tv);

tvHelloWorld.setText(hello);

return rootView;

}

}

}

正如上面代碼所示,我們使用了注解在代碼中動態的添加了數據,然後借助反射機制來獲得注解的數據;

使用了@ContentView(...) 替代了原生的setContentView(...)部分,使用了ViewUtilSvc.inject(...)進行對應的注射即可。另外,這樣的做的目的就是為了簡化對象視圖的初始化工作。具體的運行效果圖如下所示:

好了,到這裡我們的TextView上的hello world終於顯示出來了,如果有任何問題的話,請在評論中進行發問討論。

/**

* 技術交流QQ群:179914858

*/

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved