其他的就不说他多了,没什么用,有图有代码才是最好的解释
自定右边的A-Z的搜索条控件:
package com.example.pinyinretrieve;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class SideBar extends View{
private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
private int choose=-1;//是否选中
//创建画笔
private Paint paint=new Paint();
//定义拼音搜索数组
private 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", "#" };
//定义TextView控件
private TextView mTextView;
public void setmTextView(TextView mTextView) {
this.mTextView = mTextView;
}
public SideBar(Context context) {
super(context);
}
public SideBar(Context context,AttributeSet attr,int defStyle) {
super(context, attr, defStyle);
}
public SideBar(Context context,AttributeSet attr){
super(context, attr);
}
/**
* 重写该方法
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height=getHeight();//获取对应的高度
int width=getWidth();//获取对应的宽度
int bHeight=height/b.length;//获取每一个字母的搞定
for(int i=0;i<b.length;i++){
//设置画笔的颜色
paint.setColor(Color.BLACK);
//设置画笔样式
paint.setTypeface(Typeface.DEFAULT_BOLD);//默认加粗
//防止边缘锯齿
paint.setAntiAlias(true);
//设置字体大小
paint.setTextSize(20);
//如果选中设置画笔的颜色
if(i==choose){
paint.setColor(Color.parseColor("#ffffff"));
}
//获取画笔开始的位置
float xPos=width/2-paint.measureText(b[i])/2;//x坐标等于中间减去字符宽度的一半
float yPos=bHeight*i+bHeight;
//绘制一个字符
canvas.drawText(b[i], xPos, yPos, paint);
//重置画笔
paint.reset();
}
}
/**
* 从写触摸事件
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int oldChoose=choose;
float y=event.getY();//获取点击时Y坐标
OnTouchingLetterChangedListener listener=onTouchingLetterChangedListener;
//获取当前选中的位置
int c=(int) (y/getHeight()*b.length);//点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
//设置位选中
choose=-1;
if(null!=mTextView){
mTextView.setVisibility(View.GONE);//隐藏提示控件
}
//调用重绘
invalidate();
break;
default:
if(oldChoose!=c){
if(c>=0&&c<b.length){
if(null!=listener){
listener.OnTouchingLetterChanged(b[c]);//将选中的字符通过回调函数传出
}
if(null!=mTextView){
mTextView.setText(b[c]);//设置选中字母提示信息
mTextView.setVisibility(View.VISIBLE);//提示控件显示
}
choose=c;//设置选中位置
//重绘
invalidate();
}
}
break;
}
return true;//如果返回false,将接受不到该触摸事件
}
public void setOnTouchingLetterChangedListener(
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
}
/**
* 回调函数接口
* @author xiaohu
*
*/
public interface OnTouchingLetterChangedListener{
public void OnTouchingLetterChanged(String s);
}
}
布局文件代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp" />
<TextView
android:id="@+id/prompt_tv"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="@drawable/prompt_info_bk"
android:gravity="center"
android:text="A"
android:textSize="20sp"
android:visibility="gone" />
<com.example.pinyinretrieve.SideBar
android:id="@+id/sideBar"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:background="@drawable/sidebar_bk" />
</RelativeLayout>
主程序代码:
package com.example.pinyinretrieve;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView promptTv;
private SideBar sideBar;//定义拼音检索控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sideBar=(SideBar) findViewById(R.id.sideBar);//获取拼音检索控件
promptTv=(TextView) findViewById(R.id.prompt_tv);//拼音检索选中提示控件
sideBar.setmTextView(promptTv);
final TextView tv=(TextView) findViewById(R.id.tv);//定义一个控件接受从拼音检索工具中选中的字符
sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener(){
@Override
public void OnTouchingLetterChanged(String s) {
tv.setText(s);//设置该字符
}
});
}
}
只是把控件写出来了,检索功能还没有做