Android实现列表仿联系人快速查找和关键字搜索

本文介绍了如何在Android应用中实现列表的快速查找功能,包括按首字母排序并显示侧边栏导航,以及在输入框中输入关键字进行实时搜索。通过自定义ClearEditText和SideBar组件,实现清除图标显示与隐藏,以及字母栏的触摸监听。同时,使用汉字转拼音工具类实现按拼音首字母进行排序和搜索。
摘要由CSDN通过智能技术生成


列表按字母顺序排序,右侧实现根据首字母快速查找,输入框输入首字母或元素第一个字也可以实现快速查找。


页面布局如下:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:orientation="vertical">

 

   <com.example.demo.view.ClearEditText

       android:id="@+id/et"

       android:layout_width="match_parent"

       android:layout_height="50dp"

       android:layout_margin="10dp"

       android:background="@drawable/arrow_down"

       android:hint="快速查找">

   </com.example.demo.view.ClearEditText>

 

   <FrameLayout

       android:layout_width="fill_parent"

       android:layout_height="fill_parent">

 

       <ListView

           android:id="@+id/lv"

           android:layout_width="fill_parent"

           android:layout_height="fill_parent"

           android:layout_gravity="center"

           android:layout_marginLeft="10dp"

           android:layout_marginRight="20dp"

           android:divider="@null"

           android:scrollbars="none"/>

 

       <TextView

           android:id="@+id/tv"

           android:layout_width="80.0dip"

           android:layout_height="80.0dip"

           android:layout_gravity="center"

           android:background="@drawable/text_shape_bg2"

           android:gravity="center"

           android:textColor="#ffffffff"

           android:textSize="30.0dip"

           android:visibility="invisible"/>

 

       <com.example.demo.view.SideBar

           android:id="@+id/sidrbar"

           android:layout_width="30.0dip"

           android:layout_height="fill_parent"

           android:layout_gravity="right|center"/>

   </FrameLayout>

</LinearLayout>

 

在布局文件中,输入框ClearEditText和快速查找的SideBar需要自定义实现。

ClearEditText

publicclass ClearEditTextextends EditTextimplements OnFocusChangeListener,

      TextWatcher {

   /**

    *删除按钮的引用

    */

   private DrawablemClearDrawable;

 

   public ClearEditText(Context context) {

      this(context,null);

   }

 

   

   //这个构造方法也很重要,不加这个很多属性不能再XML里面定义

   public ClearEditText(Context context, AttributeSet attrs) {

      this(context, attrs, android.R.attr.editTextStyle);

   }

 

   public ClearEditText(Context context, AttributeSet attrs, int defStyle) {

      super(context, attrs, defStyle);

      init();

   }

 

   privatevoid init() {

      //获取EditText的删除文字图片,假如没有设置我们就使用默认的图片

      mClearDrawable = getCompoundDrawables()[2];

      if (mClearDrawable ==null) {

          mClearDrawable = getResources().getDrawable(

                 R.drawable.emotionstore_progresscancelbtn);

      }

      //图片被绘制的区域

      mClearDrawable.setBounds(0, 0,

             mClearDrawable.getIntrinsicWidth() / 3 * 2,

             mClearDrawable.getIntrinsicHeight() / 3 * 2);

      setClearIconVisible(false);

      setOnFocusChangeListener(this);

      addTextChangedListener(this);

   }

 

   /**

    *因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件当我们按下的位置横坐标在( EditText的宽-

    *图标到控件右边的间距-图标的宽)EditText的宽-图标到控件右边的间距)之间我们就算点击了图标,竖直方向没有考虑

    */

   @Override

   publicbooleanonTouchEvent(MotionEvent event) {

      if (getCompoundDrawables()[2] !=null) {

          if (event.getAction() == MotionEvent.ACTION_UP) {

             boolean touchable = event.getX() > (getWidth()

                    - getPaddingRight() -mClearDrawable

                        .getIntrinsicWidth())

                    &&(event.getX() < ((getWidth() - getPaddingRight())));

             if (touchable) {

                 this.setText("");

             }

          }

      }

 

      returnsuper.onTouchEvent(event);

   }

 

   /**

    *ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏

    */

   @Override

   publicvoid onFocusChange(View v,boolean hasFocus) {

      if (hasFocus) {

          setClearIconVisible(getText().length()> 0);

      }else {

          setClearIconVisible(false);

      }

   }

 

   /**

    *设置清除图标的显示与隐藏,调用setCompoundDrawablesEditText绘制上去

    *

    *@param visible

    */

   protectedvoid setClearIconVisible(boolean visible) {

      Drawable right = visible ?mClearDrawable :null;

      setCompoundDrawables(getCompoundDrawables()[0],

             getCompoundDrawables()[1], right, getCompoundDrawables()[3]);

   }

 

   /**

    *当输入框里面内容发生变化的时候回调的方法

    */

   @Override

   publicvoid onTextChanged(CharSequence s,int start,int count,int after) {

      setClearIconVisible(s.length()> 0);

   }

 

   @Override

   publicvoid beforeTextChanged(CharSequence s,int start,int count,

          int after) {

 

   }

 

   @Override

   publicvoid afterTextChanged(Editable s) {

 

   }

 

   /**

    *设置晃动动画控件晃动(根据需求)

    */

   publicvoid setShakeAnimation() {

      this.setAnimation(shakeAnimation(5));

   }

 

   /**

    *晃动动画

    *

    *@param counts

    *           1秒钟晃动多少次

    *@return

    */

   publicstatic Animation shakeAnimation(int counts) {

      AnimationtranslateAnimation =new TranslateAnimation(0, 10, 0, 0);

      translateAnimation.setInterpolator(new CycleInterpolator(counts));

      translateAnimation.setDuration(1000);

      return translateAnimation;

   }

 

SideBar

 

publicclass SideBarextends View {

   //触摸事件

   private OnTouchingLetterChangedListeneronTouchingLetterChangedListener;

   // 26个字符

   publicstatic String[]b = { "A","B","C","D","E","F","G","H","I",

          "J","K","L","M","N","O","P","Q","R","S","T"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值