[Android实例] 侧边字母滑动检索控件

来源:互联网 发布:我的世界天堂js怎么用 编辑:程序博客网 时间:2024/06/05 02:04

该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 侧边字母滑动检索控件

自定义侧边滑动检索控件,能根据A~Z字母进行检索,类似微信,通讯录右侧字母检索方式

主要代码:

@Override    protected void onDraw(Canvas canvas) {        // TODO Auto-generated method stub        super.onDraw(canvas);        // 获取焦点改变背景颜色        int height = getHeight();        int width = getWidth();        int sigleHeight = height / b.length;        for (int i = 0; i < b.length; i++) {            paint.setColor(Color.BLACK);            paint.setTypeface(Typeface.DEFAULT_BOLD);            paint.setAntiAlias(true);            paint.setTextSize(20);            // 选中状态            if (i == position) {                paint.setColor(Color.BLUE);                paint.setTextSize(30);                paint.setFakeBoldText(true);            }            // x坐标等于中间-字符串宽度的一半.            float xPos = width / 2 - paint.measureText(b[i]) / 2;            float yPos = sigleHeight * i + sigleHeight;            canvas.drawText(b[i], xPos, yPos, paint);            paint.reset();// 重置画笔        }    }    @SuppressWarnings("deprecation")    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        // TODO Auto-generated method stub        int action = event.getAction();        float y = event.getY() - getY();        int oldPosition = position;        // 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.        int c = 0;        if (y >= 0 && y <= getHeight()) {            c = (int) (y / getHeight() * b.length);        } else if (y > getHeight()) {            c = b.length - 1;        }        if (action == MotionEvent.ACTION_UP) {            setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));            position = -1;            if (showChooseText != null) {                showChooseText.setVisibility(View.INVISIBLE);            }        } else {            setBackgroundDrawable(new ColorDrawable(Color.GRAY));            setBackgroundColor(Color.GRAY);            if (oldPosition != c) {                if (c >= 0 && c < b.length) {                    if (onTouchingChangedListener != null) {                        onTouchingChangedListener.onTouchingChanged(b[c]);                    }                }                if (showChooseText != null) {                    showChooseText.setText(b[c]);                    showChooseText.setVisibility(View.VISIBLE);                }                position = c;            }        }        invalidate();        return true;    }

这里写图片描述 这里写图片描述
下载地址:项目代码

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 减肥吃了猪肉脯怎么办 吃了硬米饭胃痛怎么办 宝宝吃了硬物怎么办 米饭卡在喉咙里了怎么办 喉咙里卡了米饭怎么办 孕妇吃了坏鹅蛋怎么办 1岁大宝宝长短腿怎么办 行测中的判断推理怎么办 塑料盖子玻璃罐头瓶子打不开怎么办 猪肉烫火锅吃怎么办料 自制腊肠放干了怎么办 孕妇吃了4块腊肉怎么办 衣服沾了火锅味怎么办 皮包上有火锅味怎么办 芝士年糕裂开了怎么办 制作牛肉酱咸了怎么办 腌牛肉太咸了怎么办 八宝粥的拉环断了怎么办 八宝粥易拉罐拉环断了怎么办 吃完辣的胃难受怎么办 天天呆在家很烦怎么办 吃多了荔枝上火怎么办 猛犸牙牌子裂了怎么办 玩游戏电脑卡了怎么办 电脑打开卡在选项界面怎么办? 红警突然卡死怎么办 柯基不吃狗粮怎么办 貔貅嘴巴磕破了怎么办 开光貔貅牙磕了怎么办 玉貔貅鼻子碎了怎么办 开光的貔貅摔坏了怎么办 貔貅摔坏了耳朵怎么办? 貔貅摔坏了一点点怎么办 天猫删评价扣4分怎么办 暴风影音下载电视剧下载不了怎么办 fm2017引援电脑买了怎么办 退休时医保不够二十年怎么办 和的面迟迟不发怎么办 做面条的面发了怎么办 发不起来的面团怎么办 如果婚后买房离婚后财产怎么办