码迷,mamicode.com
首页 > 其他好文 > 详细

自定义控件

时间:2014-09-23 11:57:25      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   color   io   os   java   ar   

import java.util.LinkedList;

import com.mypower.R;

import util.OnlyTools;
import util.OnlyYouHelpMe;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

/**自定义控件
 * @ClassName SpinnerButton
 * @Description TODO 防android4.0 Spinner下拉效果
 */
public class SpinnerButton extends Button {
    /**
     * 完成选择后启动另外一个spinner
     */
    private ItemListener itemListener;
    private Context mContext;
    /** 下拉PopupWindow */
    private UMSpinnerDropDownItems mPopupWindow;
    /** 下拉布局文件 */
    private LinearLayout layout;
    /** 下拉布局文件创建监听器 */
    private ViewCreatedListener mViewCreatedListener;

    public SpinnerButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle); //环境     属性     预设的文字样式设定 
        initButton(context);//初始化
    }

    public SpinnerButton(Context context, AttributeSet attrs) {
        super(context, attrs);//环境     属性 
        initButton(context);
    }

    public SpinnerButton(Context context, final LinearLayout layout,
            ViewCreatedListener mViewCreatedListener) {//环境   现行布局   视图监听
        super(context);
        //设置下拉布局
        setResIdAndViewCreatedListener(layout, mViewCreatedListener);
        initButton(context);
    }

    private void initButton(Context context) {
        this.mContext = context;
        // UMSpinnerButton监听事件
        setOnClickListener(new UMSpinnerButtonOnClickListener());
    }

    public PopupWindow getPopupWindow() {
        return mPopupWindow;
    }

    public void setPopupWindow(UMSpinnerDropDownItems mPopupWindow) {
        this.mPopupWindow = mPopupWindow;
    }

    public LinearLayout getResId() {
        return layout;
    }

    /**
     * @Description: TODO 隐藏下拉布局
     */
    public void dismiss() {
        mPopupWindow.dismiss();
    }

    /**
     * @Description: TODO 设置下拉布局文件,及布局文件创建监听器
     * @param @param mResId 下拉布局文件ID
     * @param @param mViewCreatedListener 布局文件创建监听器
     */
    public void setResIdAndViewCreatedListener(LinearLayout layout,
            ViewCreatedListener mViewCreatedListener) {
        this.mViewCreatedListener = mViewCreatedListener;
        // 下拉布局文件id
        this.layout = layout;
        // 初始化PopupWindow
        mPopupWindow = new UMSpinnerDropDownItems(mContext);
    }

    /**
     * UMSpinnerButton的点击事件
     */
    class UMSpinnerButtonOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (mPopupWindow != null) {
                if (!mPopupWindow.isShowing()) {
                    // 设置PopupWindow弹出,退出样式
                    mPopupWindow.setAnimationStyle(R.style.Animation_dropdown);
                    // 计算popupWindow下拉x轴的位置
                    int lx = (SpinnerButton.this.getWidth()
                            - mPopupWindow.getmViewWidth() - 7) / 2;
                    // showPopupWindow
                    mPopupWindow.showAsDropDown(SpinnerButton.this, lx, -5);
                }
            }
        }
    }

    /**
     * @ClassName UMSpinnerDropDownItems
     * @Description TODO 下拉界面
     */
    public class UMSpinnerDropDownItems extends PopupWindow {

        private Context mContext;
        /** 下拉视图的宽度 */
        private int mViewWidth;
        /** 下拉视图的高度 */
        private int mViewHeight;

        public UMSpinnerDropDownItems(Context context) {
            super(context);
            this.mContext = context;
            loadViews();
        }

        /**
         * @Description: TODO 加载布局文件
         * @param
         * @return void
         * @throws
         */
        private void loadViews() {
            // 布局加载器加载布局文件
            final View v = layout;
            // 计算view宽高
            onMeasured(v);

            // 必须设置
            setWidth(LayoutParams.WRAP_CONTENT);
            setHeight(LayoutParams.WRAP_CONTENT);
            setContentView(v);
            setFocusable(true);

            // 设置布局创建监听器,以便在实例化布局控件对象
            if (mViewCreatedListener != null) {
                mViewCreatedListener.onViewCreated(v);
            }
        }

        /**
         * @Description: TODO 计算View长宽
         * @param @param v
         */
        private void onMeasured(View v) {
            int w = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            v.measure(w, h);
            mViewWidth = v.getMeasuredWidth();
            mViewHeight = v.getMeasuredHeight();
        }

        public int getmViewWidth() {
            return mViewWidth;
        }

        public void setmViewWidth(int mViewWidth) {
            this.mViewWidth = mViewWidth;
        }

        public int getmViewHeight() {
            return mViewHeight;
        }

        public void setmViewHeight(int mViewHeight) {
            this.mViewHeight = mViewHeight;
        }

    }

    /**
     * @ClassName ViewCreatedListener
     * @Description TODO 布局创建监听器,实例化布局控件对象
     * @author kenny
     * @date 2012-8-15
     */
    public interface ViewCreatedListener {
        void onViewCreated(View v);
    }

    
    public void handleClick(String[] arrayItem, int whichSpinner) {
        if (getIsShowText()) {
            this.setText(arrayItem[0]);
        }
        this.dismiss();
        itemListener.endItemSelect(arrayItem, whichSpinner);
    }

    public void setFinishListener(ItemListener itemListener) {
        this.itemListener = itemListener;
    }

    // 接口回调数据
    public static interface ItemListener {
        void endItemSelect(String[] arrayItems, int whichSpinner);
    }

    /**
     * 
     * @param arryList
     *            spinner里面的数组
     * @param whichSpinner
     *            这个是哪个spinner被选择的
     * 
     */
    public void loadListData(final LinkedList<String[]> arryList,
            final int whichSpinner) {
            this.setResIdAndViewCreatedListener(creatlayout(arryList),
                    new SpinnerButton.ViewCreatedListener() {
                        @Override
                        public void onViewCreated(View v) {
                            
                            for (int i = 0; i < arryList.size(); i++) {
                                layout.getChildAt(i).setOnClickListener(
                                        new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String[] arrayItems = new String[4];
                                                arrayItems[0] = ((TextView) v)
                                                        .getTag(R.id.first)
                                                        .toString();
                                                arrayItems[1] = ((TextView) v)
                                                        .getTag(R.id.second)
                                                        .toString();
                                                arrayItems[2] = ((TextView) v)
                                                        .getTag(R.id.three)
                                                        .toString();
                                                SpinnerButton.this.handleClick(
                                                        arrayItems,
                                                        whichSpinner);
                                            }
                                        });
                            }
                        }
                    });
    }

    /**
     * 主要设置选中某一项后是否能显示文本,默认都是显示的,你说对吧?没有哪个傻逼用sipinner不显示text的
     */
    private boolean isShowText = true;

    public boolean getIsShowText() {
        return isShowText;
    }

    public void setShowText(boolean isShowText) {
        this.isShowText = isShowText;
    }

    /**
     * 创建列表布局
     * 
     */
    public LinearLayout creatlayout(LinkedList<String[]> arryList) {
        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(Color.WHITE);
        for (int i = 0; i < arryList.size(); i++) {
            TextView tv = new TextView(getContext());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    OnlyYouHelpMe.Dp2Px(getContext(), 300),
                    OnlyYouHelpMe.Dp2Px(getContext(), 50));
            tv.setLayoutParams(params);
            tv.setTextColor(Color.BLACK);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            Drawable drawable=getResources().getDrawable(R.drawable.line);
            Drawable bg=getResources().getDrawable(R.drawable.bg_text_gray);
            tv.setBackgroundDrawable(bg);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv.setCompoundDrawables(null,null,null,drawable);
            tv.setPadding(20, 10, 10, 0);
            tv.setText(arryList.get(i)[0]);
            tv.setTag(R.id.first, arryList.get(i)[0]);
            tv.setTag(R.id.second, arryList.get(i)[1]);
            tv.setTag(R.id.three, arryList.get(i)[2]);
            layout.addView(tv);
        }
        return layout;
    }

}

 

自定义控件

标签:des   android   style   blog   color   io   os   java   ar   

原文地址:http://www.cnblogs.com/qiuyang1/p/3987751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!