package com.jiuqi.ui.widget; import java.io.Serializable; import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.jiuqi.util.UIUtil; import com.jiuqi.util.ValidateRule; import com.jiuqi.util.Validator; import com.jqyd.uilib.R; /** * 录入控件的抽象类 * * @author liyue * */ public abstract class EditorBase extends LinearLayout implements Serializable{ /** * 圆角状态 */ public static enum ROUND_STYLE{ /** * 上 */ TOP, /** * 中 */ MIDDLE, /** * 下 */ BOTTOM, /** * 所有 */ ALL } /** * 控件高度 */ public static enum LINE_HEIGHT{ /** * 正常 */ NORMAL, /** * 高(一般用于多行文本框) */ HIGH, } /** * 按钮的位置 */ public static enum BUTTON_STYLE{ /** * 左 */ LEFT, /** * 中 */ MIDDLE, /** * 右 */ RIGHT, } /** * 控件参数 */ public static class Param{ /** * 标签文字 */ public String labelText = ""; /** * 录入控件的hint文本 */ public String hintText = ""; /** * 校验规则 */ public ValidateRule[] rules; /** * 圆角风格 */ public ROUND_STYLE round_style = ROUND_STYLE.ALL; /** * 控件高度,默认为一般高度,如果是多行文本框,可以设置为LINE_HEIGHT.HIGH */ public LINE_HEIGHT line_height = LINE_HEIGHT.NORMAL; /** * 控件右侧的图标,默认没有,可以引用R.drawable.arrow_sml或者其他R.drawable.drop_down */ public int drawableRightId = -1; /** * 带按钮的编辑框的按钮位置默认是在右边 */ public BUTTON_STYLE button_style = BUTTON_STYLE.RIGHT; } /** * */ private static final long serialVersionUID = 1217823227000356538L; private ViewGroup mParent; protected Param mParam; protected Validator validator; protected LayoutInflater inflater; protected LinearLayout mWrapper; /** * 抽象类的构造函数,可以后期调用setParam()设置新参数,然后调用doInit() * @param context */ public EditorBase(Context context){ super(context); _initParam(context); } public EditorBase(Context context, AttributeSet attrs){ super(context, attrs); } /** * 构造函数 * @param context * @param param */ public EditorBase(Context context, Param param, ViewGroup parent){ super(context); this.mParam = param; this.mParent = parent; _initParam(context); doInit(); _setupWidgets(); afterInit(); } private void _initParam(Context context){ this.inflater = LayoutInflater.from(context); this.validator = new Validator(); validator.addRules(mParam.rules); createWrapper(); } private void createWrapper(){ mWrapper = new LinearLayout(getContext()); mWrapper.setOrientation(LinearLayout.HORIZONTAL); // mWrapper.setClickable(true); // mWrapper.setDuplicateParentStateEnabled(true); // mWrapper.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_editor_button)); int paddingMore = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.margin)); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); //圆角 if(ROUND_STYLE.ALL.equals(mParam.round_style)){ mWrapper.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_all)); param.setMargins(paddingMore, paddingMore, paddingMore, paddingMore); } //上圆角 else if(ROUND_STYLE.TOP.equals(mParam.round_style)){ mWrapper.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_top2)); param.setMargins(paddingMore, paddingMore, paddingMore, 0); } //中间 else if(ROUND_STYLE.MIDDLE.equals(mParam.round_style)){ mWrapper.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_middle2)); param.setMargins(paddingMore, 0, paddingMore, 0); } //下圆角 else if(ROUND_STYLE.BOTTOM.equals(mParam.round_style)){ mWrapper.setBackgroundDrawable(getResources().getDrawable(R.drawable.round_bottom)); param.setMargins(paddingMore, 0, paddingMore, paddingMore); } mWrapper.setPadding(paddingMore, paddingMore, paddingMore, paddingMore); int lineHeight = 0; if(LINE_HEIGHT.HIGH.equals(mParam.line_height)){ // lineHeight = getHugeWidgetLayoutHeightByDensity(); mWrapper.setGravity(Gravity.LEFT | Gravity.TOP); // param.height = lineHeight; }else{ mWrapper.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); } mParent.addView(mWrapper, param); } private int getScreenDensity(){ DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) System.out.println(" ******* 屏幕密度是:"+ densityDPI); return densityDPI; } // private int getWidgetLayoutHeightByDensity(){ // int density = getScreenDensity(); // int height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.smallButtonHeight240)); // switch (density) { // case 120: // break; // case 160: // height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.smallButtonHeight160)); // break; // case 240: // height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.smallButtonHeight240)); // break; // case 320: // height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.smallButtonHeight320)); // break; // case 480: // height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.smallButtonHeight480)); // break; // case 720: // // break; // default: // break; // // } // // return height; // } private int getHugeWidgetLayoutHeightByDensity(){ int density = getScreenDensity(); int height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.hugeButtonHeight240)); switch (density) { case 120: break; case 160: height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.hugeButtonHeight160)); break; case 240: height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.hugeButtonHeight240)); break; case 320: height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.hugeButtonHeight320)); break; case 480: height = UIUtil.dip2px(getContext(), getResources().getDimension(R.dimen.hugeButtonHeight480)); break; case 720: break; default: break; } return height; } public void setViewButtomHeight(){ RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams)getInputWidget().getLayoutParams(); lp.setMargins(0, 0, UIUtil.dip2px(getContext(),getResources().getDimension(R.dimen.marginMax)), UIUtil.getWidgetLayoutBottomHeightByDensity(getContext())); this.getInputWidget().setLayoutParams(lp); } /** * 设置控件 */ private void _setupWidgets(){ if(null != getLabel()){ getLabel().setText(mParam.labelText); } if(null != getInputWidget()){ if(getInputWidget() instanceof TextView){ ((TextView)getInputWidget()).setHint(this.mParam.hintText); } } } /** * 初始化之后 */ protected void afterInit(){ } /** * 初始化 */ protected void doInit(){ } /** * 得到右侧的控件 * @return */ public abstract View getInputWidget(); /** * 得到左侧的标签控件 * @return */ public abstract TextView getLabel(); /** * 初始化 * @param param */ public void setParam(Param param){ this.mParam = param; } /** * 校验,通过返回true,不通过返回false。 * 默认根据参数的正则表达式进行校验,如果expValidate为空,则返回true。 * 如果需要界面自己校验,可以重写此方法 * @return boolean */ public boolean validate(){ if(mParam.rules == null || mParam.rules.length == 0){ return true; } else{ if(getInputWidget() instanceof TextView){ TextView tv = (TextView)getInputWidget(); if(TextUtils.isEmpty(tv.getText())){ return false; } else{ return validator.validate(tv.getText()); } } else{ return false; } } } }