package org.gulup.annotation; /** * @author gulup * @version 创建时间:2014-5-10 下午1:15:26 * 类说明:控件資源查找類 */ import android.app.Activity; import android.content.Context; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceGroup; import android.view.View; public class ViewFinder { private View view; private Context context; private Activity activity; private PreferenceGroup preferenceGroup; private PreferenceActivity preferenceActivity; public ViewFinder(View view) { this.view = view; } public ViewFinder(View view,Context context) { this.view = view; this.context = context; } public ViewFinder(Activity activity) { this.activity = activity; } public ViewFinder(PreferenceGroup preferenceGroup) { this.preferenceGroup = preferenceGroup; } public ViewFinder(PreferenceActivity preferenceActivity) { this.preferenceActivity = preferenceActivity; this.activity = preferenceActivity; } public View findViewById(int id) { return activity == null ? view.findViewById(id) : activity .findViewById(id); } public View findViewById(String name,String defType) { return activity == null ? view.findViewById(context.getResources().getIdentifier(name, defType, context.getPackageName())) : activity .findViewById(activity.getResources().getIdentifier(name, defType, activity.getPackageName())); } public View findViewByInfo(ViewInjectInfo info) { return findViewById((Integer) info.value, info.parentId); } public View findViewById(int id, int pid) { View pView = null; if (pid > 0) { pView = this.findViewById(pid); } View view = null; if (pView != null) { view = pView.findViewById(id); } else { view = this.findViewById(id); } return view; } public View findViewById(String name,String defType, int pid) { View pView = null; if (pid > 0) { pView = this.findViewById(pid); } View view = null; if (pView != null) { view = pView.findViewById(activity.getResources().getIdentifier(name, defType, activity.getPackageName())); } else { view = this.findViewById(name,defType); } return view; } @SuppressWarnings("deprecation") public Preference findPreference(CharSequence key) { return preferenceGroup == null ? preferenceActivity.findPreference(key) : preferenceGroup.findPreference(key); } public Context getContext() { if (view != null) return view.getContext(); if (activity != null) return activity; if (preferenceActivity != null) return preferenceActivity; return null; } }