最近需要实现一个手机通讯录的快速索引功能。根据姓名首字母快速索引功能。下面是一个手机联系人快速索引的效果,总体来说代码不算难,拼音转换的地方略有复杂。下面上源码:源码中有注释。
最近需要实现一个手机通讯录的快速索引功能。根据姓名首字母快速索引功能。下面是一个手机联系人快速索引的效果,总体来说代码不算难,拼音转换的地方略有复杂。下面上源码:源码中有注释。
下面是效果图:
MainActivity:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import Android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv_main;
private FriendAdapter adapter;
private List<Friend> data = new ArrayList<Friend>();
private QuickIndexView qiv_main;
private TextView tv_main_Word;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
//隐藏word
tv_main_word.setVisibility(View.GoNE);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main = (ListView) findViewById(R.id.lv_main);
qiv_main = (QuickIndexView) findViewById(R.id.qiv_main);
tv_main_word = (TextView) findViewById(R.id.tv_main_word);
//设置监听
qiv_main.setOnIndexChangedListener(new QuickIndexView.OnIndexChangedListener() {
@Override
public void onIndexChanged(String word) {
tv_main_word.setText(word);
tv_main_word.setVisibility(View.VISIBLE);
//handler.removeMessages(1);
//移除未处理的消息
handler.removeCallbacksAndMessages(null);
//发延迟消息
handler.sendEmptyMessageDelayed(1, 2000);
//滑动listview
//查找对应的item
for(int i=0;i<data.size();i++) {
String fWord = data.get(i).getPinyin().substring(0, 1);
if(word.equals(fWord)) {
lv_main.setSelection(i);
return;
}
}
}
@Override
public void onUp() {
//tv_main_word.setVisibility(View.GONE);
}
});
//显示列表
adapter = new FriendAdapter();
initData();
lv_main.setAdapter(adapter);
//lv_main.setSelection(5);
}
private void initData() {
data.add(new Friend("张三"));
data.add(new Friend("杨九"));
data.add(new Friend("胡继群"));
data.add(new Friend("刘畅"));
data.add(new Friend("钟泽兴"));
data.add(new Friend("尹革新"));
data.add(new Friend("安传鑫"));
data.add(new Friend("张骞壬"));
data.add(new Friend("温松"));
data.add(new Friend("李凤秋"));
data.add(new Friend("刘甫"));
data.add(new Friend("娄全超"));
data.add(new Friend("张猛"));
data.add(new Friend("王英杰"));
data.add(new Friend("李振南"));
data.add(new Friend("孙仁政"));
data.add(new Friend("唐春雷"));
data.add(new Friend("牛鹏伟"));
data.add(new Friend("姜宇航"));
data.add(new Friend("刘挺"));
data.add(new Friend("张洪瑞"));
data.add(new Friend("张建忠"));
data.add(new Friend("侯亚帅"));
data.add(new Friend("刘帅"));
data.add(new Friend("乔竞飞"));
data.add(new Friend("徐雨健"));
data.add(new Friend("吴亮"));
data.add(new Friend("王兆霖"));
data.add(new Friend("阿三"));
Collections.sort(data);
}
class FriendAdapter extends BaseAdapter {
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null) {
holder = new ViewHolder();
convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
holder.wordTV = (TextView) convertView.findViewById(R.id.tv_item_word);
holder.nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
convertView.setTag(holder);/
public class QuickIndexView extends View {
private float itemWidth;
private float itemHeight;
// private float wordWidth;
// private float wordHeight;
private String[] indexArr = { "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 Paint paint;
public QuickIndexView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(16);
paint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
itemWidth = this.getMeasuredWidth();
itemHeight = this.getMeasuredHeight() / 26f;
}
@Override
protected void onDraw(canvas canvas) {
//当每次触发重绘的时候,就把26个字母循环一遍
for (int i = 0; i < indexArr.length; i++) {
String word = indexArr[i];
// 设置文字的颜色
if (i == touchIndex) {
//这里设置被点击的字母变化:颜色变灰色、字体变25sp
paint.setColor(Color.GRAY);
paint.setTextSize(25);
} else {
//其他没被点击的字母,保持原有状态:设置颜色、字体大小为18sp
paint.setColor(Color.BLACK);
paint.setTextSize(18);
}
// 得到word的宽高
Rect bounds = new Rect();
paint.getTextBounds(word, 0, word.length(), bounds);
//得到字体的宽
int wordWidth = bounds.width();
//得到字体的高
int wordHeight = bounds.height();
// 计算word的左上角的坐标:字母所在的X坐标、Y坐标
float x = itemWidth / 2 - wordWidth / 2;
float y = itemHeight / 2 + wordHeight / 2 + i * itemHeight;
// 绘制word
canvas.drawText(word, x, y, paint);
}
}
// ///////////////////////////////////////////////////////////////////////
private int touchIndex = -1;// 触摸的字母的下标
@Override
public boolean onTouchEvent(MotionEvent event) {
// 得到事件坐标
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
// 计算下标
int index = (int) (eventY / itemHeight);
if (index > 25) {
index = 25;
}
if (index < 0) {
index = 0;
}
// 如果下标有改变, 强制重绘
if (index != touchIndex) {
// 更新touchIndex
touchIndex = index;
// 强制重绘
invalidate();
// 通知Activity更新TextView
if (onIndexChangedListener != null) {
onIndexChangedListener.onIndexChanged(indexArr[index]);
}
}
break;
case MotionEvent.ACTION_UP:
touchIndex = -1;
// 强制重绘
invalidate();
// 通知Activity更新TextView
if (onIndexChangedListener != null) {
onIndexChangedListener.onUp();
}
break;
default:
break;
}
return true;// 所有的事件都由当前视图消费
}
private OnIndexChangedListener onIndexChangedListener;
public void setOnIndexChangedListener(
OnIndexChangedListener onIndexChangedListener) {
this.onIndexChangedListener = onIndexChangedListener;
}
interface OnIndexChangedListener {
// 当操作的下标改变时自动调用
public void onIndexChanged(String word);
// 当up时调用
public void onUp();
}
}
联系人类:
public class Friend implements Comparable<Friend> {
private String name;
private String pinyin;
public Friend(String name) {
super();
this.name = name;
pinyin = PinYinUtils.getPinYin(name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinyin() {
return pinyin;
}
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}
@Override
public String toString() {
return "Friend [name=" + name + ", pinyin=" + pinyin + "]";
}
@Override
public int compareTo(Friend another) {
return this.pinyin.compareTo(another.getPinyin());
}
}
工具类:用于将汉字转换为拼音
public class PinYinUtils {
public static String getPinYin(String hanzi){
String pinyin = "";
HanyuPinyinOutputFORMat format = new HanyuPinyinOutputFormat();//控制转换是否大小写,是否带音标
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);//大写
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//由于不能直接对多个汉字转换,只能对单个汉字转换
char[] arr = hanzi.toCharArray();
for (int i = 0; i < arr.length; i++) {
if(Character.isWhitespace(arr[i]))continue;//如果是空格,则不处理,进行下次遍历
//汉字是2个字节存储,肯定大于127,所以大于127就可以当为汉字转换
if(arr[i]>127){
try {
//由于多音字的存在,单 dan shan
String[] pinyinArr = PinyinHelper.toHanyuPinyinStringArray(arr[i], format);
if(pinyinArr!=null){
pinyin += pinyinArr[0];
}else {
pinyin += arr[i];
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
//不是正确的汉字
pinyin += arr[i];
}
}else {
//不是汉字,
pinyin += arr[i];
}
}
return pinyin;
}
}
以上代码是关于Android手机联系人快速索引(手机通讯录)的全部叙述,希望大家喜欢。
--结束END--
本文标题: Android手机联系人快速索引(手机通讯录)
本文链接: https://lsjlt.com/news/26170.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0