主要是是自定义View将所有字母画出来,然后对dispatchTouchEvent 处理滑动事件,具体看代码吧!
public class MyLetterView extends View {
public static String[] b = {"#", "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"};
private int choose = -1;
private Paint paint = new Paint();
private int mWidth = 0;
private int mHeight = 0;
private int normalTextSize = 22;
private int normalTextColor;
private int pressedTextColor;
private int normalBgColor;
private int pressedBgColor;
private boolean pressed = false;
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
public MyLetterView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MyLetterView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
normalTextColor = context.getResources().getColor(R.color.blue_normal);
pressedTextColor = context.getResources().getColor(R.color.blue_pressed);
normalBgColor = context.getResources().getColor(R.color.white_color);
pressedBgColor = context.getResources().getColor(R.color.white_color);
this.setBackgroundColor(normalBgColor);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = reset(paint);
if (pressed) {
canvas.drawColor(pressedBgColor);
}
int singleHeight = mHeight / b.length;//每个字母范围所占的高度
for (int i = 0; i < b.length; i++) {
paint.setColor(normalTextColor);
if (i == choose) {
paint.setColor(pressedTextColor);
paint.setFakeBoldText(true);
}
//写字的 坐标
float xPos = (mWidth - paint.measureText(b[i])) / 2;//横向居中
float yPos = singleHeight * i + singleHeight;//纵向居中
canvas.drawText(b[i], xPos, yPos, paint);
}
}
/**
* 重置画笔
*
* @param paint
* @return
*/
private Paint reset(Paint paint) {
//配置画笔
paint.setAntiAlias(true);
paint.setTextSize(normalTextSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
return paint;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
pressed = true;
if (oldChoose != c && listener != null) {
if (c > 0 && c < b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null) {
if (c > 0 && c < b.length) {
listener.onTouchingLetterChanged(b[c]);
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
pressed = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
public interface OnTouchingLetterChangedListener {
void onTouchingLetterChanged(String s);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mWidth = this.getWidth();
mHeight = this.getHeight();
}
}
效果

然后view放到你需要的地方就能使用啦!!!