【实例简介】全国城市 sqlite数据库在这个目录 res/raw/china_city
【实例截图】
【核心代码】
package com.city.list.main;
t.Context;
aphics.Canvas;
aphics.Color;
aphics.Paint;
aphics.Typeface;
style.TypefaceSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyLetterListView extends View
{
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
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" };
int choose = -1;
Paint paint = new Paint();
boolean showBkg = false;
public MyLetterListView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public MyLetterListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyLetterListView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
if (showBkg)
{
canvas.drawColor(Color.parseColor("#40000000"));
}
int height = getHeight();
int width = getWidth();
int singleHeight = height / b.length;
for (int i = 0; i < b.length; i )
{
paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(25);
if (i == choose)
{
paint.setColor(Color.parseColor("#3399ff"));
paint.setFakeBoldText(true);
}
float xPos = width / 2 - asureText(b[i]) / 2;
float yPos = singleHeight * i singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event)
{
final int action = Action();
final float y = Y();
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);
switch (action) {
case MotionEvent.ACTION_DOWN:
showBkg = true;
if (oldChoose != c && listener != null)
{
if (c > 0 && c < b.length)
{
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_MOVE:
if (oldChoose != c && listener != null)
{
if (c > 0 && c < b.length)
{
choose = c;
invalidate();
}
}
break;
case MotionEvent.ACTION_UP:
showBkg = false;
choose = -1;
invalidate();
break;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
TouchEvent(event);
}
public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener)
{
}
public interface OnTouchingLetterChangedListener
{
public void onTouchingLetterChanged(String s);
}
}
本文发布于:2024-02-01 17:21:41,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170678038538231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |