Java tutorial
/* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.bangqu.eshow.activity; import android.app.Application; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AbsListView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.RelativeLayout; import android.widget.Toast; import com.bangqu.eshow.global.ESActivityManager; import com.bangqu.eshow.view.ioc.ESIocEventListener; import com.bangqu.eshow.view.ioc.ESIocSelect; import com.bangqu.eshow.view.ioc.ESIocView; import com.bangqu.eshow.view.titlebar.ESBottomBar; import com.bangqu.eshow.view.titlebar.ESTitleBar; import java.lang.reflect.Field; // TODO: Auto-generated Javadoc /** * ActivityActivity?. */ public abstract class ESActivity extends FragmentActivity { /** LayoutInflater???. */ public LayoutInflater mInflater; /** Application???. */ public Application abApplication = null; /** . */ public RelativeLayout ab_base = null; /** ?. */ private ESTitleBar mAbTitleBar = null; /** ?. */ private ESBottomBar mAbBottomBar = null; /** . */ protected RelativeLayout contentLayout = null; /** * ??. * * @param savedInstanceState the saved instance state * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); mInflater = LayoutInflater.from(this); //? mAbTitleBar = new ESTitleBar(this); // ab_base = new RelativeLayout(this); ab_base.setBackgroundColor(Color.rgb(255, 255, 255)); // contentLayout = new RelativeLayout(this); contentLayout.setPadding(0, 0, 0, 0); //? mAbBottomBar = new ESBottomBar(this); //View ab_base.addView(mAbTitleBar, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); mAbTitleBar.setVisibility(View.GONE); RelativeLayout.LayoutParams layoutParamsBottomBar = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsBottomBar.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); ab_base.addView(mAbBottomBar, layoutParamsBottomBar); RelativeLayout.LayoutParams layoutParamsContent = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsContent.addRule(RelativeLayout.BELOW, mAbTitleBar.getId()); layoutParamsContent.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId()); ab_base.addView(contentLayout, layoutParamsContent); //Application? abApplication = getApplication(); //ContentView setContentView(ab_base, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); ESActivityManager.getInstance().addActivity(this); } /** * ??View?. * @param contentView View */ public void setAbContentView(View contentView) { contentLayout.removeAllViews(); contentLayout.addView(contentView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); initIocView(); } /** * ???IDView?. * @param resId View?ID */ public void setAbContentView(int resId) { setAbContentView(mInflater.inflate(resId, null)); } /** * ??. * @return the title layout */ public ESTitleBar getTitleBar() { mAbTitleBar.setVisibility(View.VISIBLE); return mAbTitleBar; } /** * ??. * @return the bottom layout */ public ESBottomBar getBottomBar() { return mAbBottomBar; } /** * ?????. * * @param overlay the new title bar overlay */ public void setTitleBarOverlay(boolean overlay) { ab_base.removeAllViews(); if (overlay) { RelativeLayout.LayoutParams layoutParamsFW1 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsFW1.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId()); ab_base.addView(contentLayout, layoutParamsFW1); RelativeLayout.LayoutParams layoutParamsFW2 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsFW2.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); ab_base.addView(mAbTitleBar, layoutParamsFW2); RelativeLayout.LayoutParams layoutParamsFW3 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsFW3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); ab_base.addView(mAbBottomBar, layoutParamsFW3); } else { ab_base.addView(mAbTitleBar, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); RelativeLayout.LayoutParams layoutParamsFW2 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsFW2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); ab_base.addView(mAbBottomBar, layoutParamsFW2); RelativeLayout.LayoutParams layoutParamsFW1 = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParamsFW1.addRule(RelativeLayout.BELOW, mAbTitleBar.getId()); layoutParamsFW1.addRule(RelativeLayout.ABOVE, mAbBottomBar.getId()); ab_base.addView(contentLayout, layoutParamsFW1); } } /** * ????. * * @param layoutResID the new content view * @see android.app.Activity#setContentView(int) */ @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); initIocView(); } /** * ????. * * @param view the view * @param params the params * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams) */ @Override public void setContentView(View view, android.view.ViewGroup.LayoutParams params) { super.setContentView(view, params); } /** * ????. * * @param view the new content view * @see android.app.Activity#setContentView(android.view.View) */ public void setContentView(View view) { super.setContentView(view); } /** * ?IOCView. */ private void initIocView() { Field[] fields = getClass().getDeclaredFields(); if (fields != null && fields.length > 0) { for (Field field : fields) { try { field.setAccessible(true); if (field.get(this) != null) continue; ESIocView viewInject = field.getAnnotation(ESIocView.class); if (viewInject != null) { int viewId = viewInject.id(); field.set(this, findViewById(viewId)); setListener(field, viewInject.click(), ESIocEventListener.CLICK); setListener(field, viewInject.longClick(), ESIocEventListener.LONGCLICK); setListener(field, viewInject.itemClick(), ESIocEventListener.ITEMCLICK); setListener(field, viewInject.itemLongClick(), ESIocEventListener.ITEMLONGCLICK); ESIocSelect select = viewInject.select(); if (!TextUtils.isEmpty(select.selected())) { setViewSelectListener(field, select.selected(), select.noSelected()); } } } catch (Exception e) { e.printStackTrace(); } } } } /** * view?. * * @param field the field * @param select the select * @param noSelect the no select * @throws Exception the exception */ private void setViewSelectListener(Field field, String select, String noSelect) throws Exception { Object obj = field.get(this); if (obj instanceof View) { ((AbsListView) obj) .setOnItemSelectedListener(new ESIocEventListener(this).select(select).noSelect(noSelect)); } } /** * view?. * * @param field the field * @param methodName the method name * @param method the method * @throws Exception the exception */ private void setListener(Field field, String methodName, int method) throws Exception { if (methodName == null || methodName.trim().length() == 0) return; Object obj = field.get(this); switch (method) { case ESIocEventListener.CLICK: if (obj instanceof View) { ((View) obj).setOnClickListener(new ESIocEventListener(this).click(methodName)); } break; case ESIocEventListener.ITEMCLICK: if (obj instanceof AbsListView) { ((AbsListView) obj).setOnItemClickListener(new ESIocEventListener(this).itemClick(methodName)); } break; case ESIocEventListener.LONGCLICK: if (obj instanceof View) { ((View) obj).setOnLongClickListener(new ESIocEventListener(this).longClick(methodName)); } break; case ESIocEventListener.ITEMLONGCLICK: if (obj instanceof AbsListView) { ((AbsListView) obj) .setOnItemLongClickListener(new ESIocEventListener(this).itemLongClick(methodName)); } break; default: break; } } /** * * @param view */ public void back(View view) { finish(); } /** * ??Activity?. * * @see android.app.Activity#finish() */ @Override public void finish() { ESActivityManager.getInstance().removeActivity(this); super.finish(); } /** * Toast short ?? * * @param content */ public void showShortToast(Context mContext, String content) { if (null != mContext && null != content && content.length() > 1) { Toast.makeText(mContext, content, Toast.LENGTH_SHORT).show(); } } /** * Toast long ?? * * @param content */ public void showLongToast(Context mContext, String content) { Toast.makeText(mContext, content, Toast.LENGTH_LONG).show(); } }