android选择城市ListView 城市ListView 按字母分类展示,右侧是A-Z 滑动选择,类似于iphone通讯之类的效果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class SideBar extends View { private char[] l; private SectionIndexer sectionIndexter = null; private ListView list; private final int m_nItemHeight = 22; public SideBar(Context context) { super(context); init(); } public SideBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { l = new char[] { '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' }; setBackgroundColor(0x44FFFFFF); } public SideBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public void setListView(ListView _list) { list = _list; sectionIndexter = (SectionIndexer) _list.getAdapter(); } public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int i = (int) event.getY(); int idx = i / m_nItemHeight; if (idx >= l.length) { idx = l.length - 1; } else if (idx < 0) { idx = 0; } if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { if (sectionIndexter == null) { sectionIndexter = (SectionIndexer) list.getAdapter(); } int position = sectionIndexter.getPositionForSection(l[idx]); if (position == -1) { return true; } list.setSelection(position); } return true; } protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(0xFFA6A9AA); paint.setTextSize(16); paint.setTextAlign(Paint.Align.CENTER); float widthCenter = getMeasuredWidth() / 2; for (int i = 0; i < l.length; i++) { canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight + (i * m_nItemHeight), paint); } super.onDraw(canvas); }}public class HzToPinyin { /** * * [汉字转拼音] * * @param src * 欲转换的汉字 * @return [汉字的拼音的首字母] * @createTime 2012-1-4 下午03:56:31 */ public static String getPingYin(String src) { char[] t1 = null; t1 = src.toCharArray(); String[] t2 = new String[t1.length]; HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat(); t3.setCaseType(HanyuPinyinCaseType.LOWERCASE); t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE); t3.setVCharType(HanyuPinyinVCharType.WITH_V); String t4 = ""; int t0 = t1.length; try { for (int i = 0; i < t0; i++) { // 判断是否为汉字字符 if (java.lang.Character.toString(t1[i]).matches( "[\\u4E00-\\u9FA5]+")) { t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3); t4 += t2[0]; } else t4 += java.lang.Character.toString(t1[i]); } System.out.println(t4); return t4; } catch (BadHanyuPinyinOutputFormatCombination e1) { e1.printStackTrace(); } return t4; } // 返回中文的首字母 public static String getPinYinHeadChar(String str) { String convert = ""; for (int j = 0; j < str.length(); j++) { char word = str.charAt(j); String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word); if (pinyinArray != null) { convert += pinyinArray[0].charAt(0); } else { convert += word; } } return convert; } // 将字符串转移为ASCII码 public static String getCnASCII(String cnStr) { StringBuffer strBuf = new StringBuffer(); byte[] bGBK = cnStr.getBytes(); for (int i = 0; i < bGBK.length; i++) { // System.out.println(Integer.toHexString(bGBK[i]&0xff)); strBuf.append(Integer.toHexString(bGBK[i] & 0xff)); } return strBuf.toString(); }} <com.wisedu.hanlinyuan.widget.SideBar android:id="@+id/sideBar" android:layout_width="20dip" android:layout_marginRight="10dip" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/new_movement_select_all" />再去下载个pinyin4j.jar就ok了 妹子是android新手,遇到数据传递问题,求少年们帮助 置顶问题??? acitivtyA启动后模拟home键隐藏,需要在acitivtyA中启动acitivtyB并让其显示,如何实现呢? 构造函数和onCreate()函数有什么区别? 请问一个问题:ImageButton的背景图片重叠 类似于“天天动听”从互联网上下载mp3文件,求思路?? 请问大家一个很基础的问题: 大家请进来,急需帮助。 基于androids的手机计算器设计 自定义注解处理器未触发 CallLog.Calls.DATE格式化问题 怎样实现Android屏幕的自动左右滑动?
private char[] l;
private SectionIndexer sectionIndexter = null;
private ListView list;
private final int m_nItemHeight = 22; public SideBar(Context context) {
super(context);
init();
} public SideBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} private void init() {
l = new char[] { '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' };
setBackgroundColor(0x44FFFFFF);
} public SideBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} public void setListView(ListView _list) {
list = _list;
sectionIndexter = (SectionIndexer) _list.getAdapter();
} public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
int i = (int) event.getY();
int idx = i / m_nItemHeight;
if (idx >= l.length) {
idx = l.length - 1;
} else if (idx < 0) {
idx = 0;
}
if (event.getAction() == MotionEvent.ACTION_DOWN
|| event.getAction() == MotionEvent.ACTION_MOVE) {
if (sectionIndexter == null) {
sectionIndexter = (SectionIndexer) list.getAdapter();
}
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) {
return true;
}
list.setSelection(position);
}
return true;
} protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(0xFFA6A9AA);
paint.setTextSize(16);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / 2;
for (int i = 0; i < l.length; i++) {
canvas.drawText(String.valueOf(l[i]), widthCenter, m_nItemHeight
+ (i * m_nItemHeight), paint);
}
super.onDraw(canvas);
}
}
public class HzToPinyin {
/**
*
* [汉字转拼音]
*
* @param src
* 欲转换的汉字
* @return [汉字的拼音的首字母]
* @createTime 2012-1-4 下午03:56:31
*/
public static String getPingYin(String src) { char[] t1 = null;
t1 = src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4 = "";
int t0 = t1.length;
try {
for (int i = 0; i < t0; i++) {
// 判断是否为汉字字符
if (java.lang.Character.toString(t1[i]).matches(
"[\\u4E00-\\u9FA5]+")) {
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4 += t2[0];
} else
t4 += java.lang.Character.toString(t1[i]);
}
System.out.println(t4);
return t4;
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
} // 返回中文的首字母
public static String getPinYinHeadChar(String str) { String convert = "";
for (int j = 0; j < str.length(); j++) {
char word = str.charAt(j);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert += pinyinArray[0].charAt(0);
} else {
convert += word;
}
}
return convert;
} // 将字符串转移为ASCII码
public static String getCnASCII(String cnStr) {
StringBuffer strBuf = new StringBuffer();
byte[] bGBK = cnStr.getBytes();
for (int i = 0; i < bGBK.length; i++) {
// System.out.println(Integer.toHexString(bGBK[i]&0xff));
strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
}
return strBuf.toString();
}} <com.wisedu.hanlinyuan.widget.SideBar
android:id="@+id/sideBar"
android:layout_width="20dip"
android:layout_marginRight="10dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/new_movement_select_all" />再去下载个pinyin4j.jar就ok了