Android 自定义字母搜索表
来源:互联网 发布:哪下载软件安全 编辑:程序博客网 时间:2024/05/17 08:00
这是我写的一个自定义VIew: 联系人字母搜索表
首先,设置atrss
设置的attr标签下可以设置选中或者未选中的字母表中字母的颜色,若为设置,在view类中有默认的颜色
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="LetterView"> <attr name="select_color" format="color"/> <attr name="unselect" format="color"/> </declare-styleable></resources>
接下来是自定义view类的实例代码
public class LetterView extends View{ private Paint paint; private int count; private int single; private int chooseLetter; private TextView bigLetter; private int chooseColor; private int defaultColor; private static final String[] LETTERS = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" , "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; public LetterView(Context context) { this(context, null); } public LetterView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public LetterView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //获取自定义属性 1 AttributeSet属性的集合 2 自定义的属性 3默认的属性值 4默认的资源 TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LetterView, defStyleAttr, 0); defaultColor = typedArray.getColor(R.styleable.LetterView_unselect, Color.BLACK); chooseColor = typedArray.getColor(R.styleable.LetterView_select_color, Color.RED); typedArray.recycle(); paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(20); chooseLetter = -1; } //获取导航显示大字母的View public void setBigLetter(TextView bigLetter){ this.bigLetter = bigLetter; bigLetter.setVisibility(GONE); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); //获取View的高度和宽度 int height = getHeight(); int width = getWidth(); //计算一个字母的高度 count = LETTERS.length; single = height/count; for(int i = 0; i < count; i++){ //计算一个字母的宽度 int x = (int)(paint.measureText(LETTERS[i])); if(i == chooseLetter){ paint.setColor(chooseColor); paint.setTextSize(40); } else { paint.setColor(defaultColor); paint.setTextSize(20); } canvas.drawText(LETTERS[i], width/2-x/2, (i+1) * single, paint); } } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: //获取点击或触摸的位置的Y坐标 float y = event.getY(); int position = (int)(y / single); if(position >= 0 && position < LETTERS.length){ chooseLetter = position; bigLetter.setVisibility(VISIBLE); bigLetter.setText(LETTERS[chooseLetter]); invalidate(); } break; case MotionEvent.ACTION_UP: chooseLetter = -1; bigLetter.setVisibility(GONE); invalidate(); break; } return true; }}
0 0
- Android 自定义字母搜索表
- 自定义tableview索引,字母搜索
- android 按字母搜索
- Android自定义搜索框
- Android 自定义搜索框
- Android自定义屏幕侧边字母排序控件
- Android 自定义View字母滑动索引
- Android 自定义View 字母索引条
- Android自定义View实现字母索引条
- Android自定义键盘:数字键盘和字母键盘
- android自定义view 右侧字母导航
- android 自定义搜索框 + 搜索提示
- 利用UITextField自定义搜索栏,实现中文输入过程中字母的搜索功能
- Android 自定义过滤搜索框
- Android 自定义过滤搜索框
- Android 自定义过滤搜索框
- Android 自定义过滤搜索框
- Android之自定义搜索框
- 嵌入式dvfs动态调频调压技术
- 关于结对编程
- java---转换流1(InputStreamReader和OutputStreamWriter)
- 欢迎使用CSDN-markdown编辑器
- hdoj 5672 String (尺取法)
- Android 自定义字母搜索表
- 进制转换(随意转)
- 页 页表 页表项
- java泛型(二)、泛型的内部原理:类型擦除以及类型擦除带来的问题
- c#之插入排序的实现
- github上的webrtc
- 编译原理----基础概念
- 初识Leap motion
- 基于QT和opencv的瞳孔定位及跟踪程序