android通訊錄列表,A~Z字母提示(修正版)

來源:互聯網
上載者:User

先看效果

 

 

 

首字母提示的view,AlphaView:


[java]
package net.alpha; 
 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.ImageView; 
 
public class AlphaView extends ImageView { 
    private Drawable alphaDrawable; 
    private boolean showBkg; // 是否顯示背景  
    private int choose; // 當前選中首字母的位置  
    private String[] ALPHAS; 
    private OnAlphaChangedListener listener; 
 
    public AlphaView(Context context) { 
        super(context); 
        initAlphaView(); 
    } 
 
    public AlphaView(Context context, AttributeSet attrs) { 
        super(context, attrs); 
        initAlphaView(); 
    } 
 
    public AlphaView(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        initAlphaView(); 
    } 
 
    private void initAlphaView() { 
        showBkg = false; 
        choose = -1; 
        setImageResource(R.drawable.alpha_normal); 
        alphaDrawable = getDrawable(); 
         
        ALPHAS = new String[28]; 
        ALPHAS[0] = " "; // " "代表搜尋  
        ALPHAS[27] = "#"; 
        for (int i = 0; i < 26; i++) { 
            ALPHAS[i + 1] = String.valueOf((char) (65 + i)); 
        } 
    } 
 
    @Override 
    protected void onDraw(Canvas canvas) { 
        if (showBkg) { 
            setImageResource(R.drawable.alpha_pressed); 
            alphaDrawable = getDrawable(); 
 
            alphaDrawable.setBounds(0, 0, getWidth(), getHeight()); 
        } else { 
            setImageResource(R.drawable.alpha_normal); 
            alphaDrawable = getDrawable(); 
 
            alphaDrawable.setBounds(0, 0, getWidth(), getHeight()); 
        } 
 
        canvas.save(); 
        alphaDrawable.draw(canvas); 
        canvas.restore(); 
    } 
 
    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
        final float y = event.getY(); 
        final int oldChoose = choose; 
        final int c = (int) (y / getHeight() * 28); 
 
        switch (event.getAction()) { 
        case MotionEvent.ACTION_DOWN: 
            showBkg = true; 
            if (oldChoose != c && listener != null) { 
                if (c >= 0 && c < ALPHAS.length) { 
                    listener.OnAlphaChanged(ALPHAS[c], c); 
                    choose = c; 
                } 
            } 
            invalidate(); 
            break; 
 
        case MotionEvent.ACTION_MOVE: 
            if (oldChoose != c && listener != null) { 
                if (c >= 0 && c < ALPHAS.length) { 
                    listener.OnAlphaChanged(ALPHAS[c], c); 
                    choose = c; 
                } 
            } 
            invalidate(); 
            break; 
 
        case MotionEvent.ACTION_UP: 
            showBkg = false; 
            choose = -1; 
            invalidate(); 
            break; 
        } 
        return true; 
    } 
 
    // 設定事件  
    public void setOnAlphaChangedListener(OnAlphaChangedListener listener) { 
        this.listener = listener; 
    } 
 
    // 事件介面  
    public interface OnAlphaChangedListener { 
        public void OnAlphaChanged(String s, int index); 
    } 
 

package net.alpha;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class AlphaView extends ImageView {
 private Drawable alphaDrawable;
 private boolean showBkg; // 是否顯示背景
 private int choose; // 當前選中首字母的位置
 private String[] ALPHAS;
 private OnAlphaChangedListener listener;

 public AlphaView(Context context) {
  super(context);
  initAlphaView();
 }

 public AlphaView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initAlphaView();
 }

 public AlphaView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  initAlphaView();
 }

 private void initAlphaView() {
  showBkg = false;
  choose = -1;
  setImageResource(R.drawable.alpha_normal);
  alphaDrawable = getDrawable();
  
  ALPHAS = new String[28];
  ALPHAS[0] = " "; // " "代表搜尋
  ALPHAS[27] = "#";
  for (int i = 0; i < 26; i++) {
   ALPHAS[i + 1] = String.valueOf((char) (65 + i));
  }
 }

 @Override
 protected void onDraw(Canvas canvas) {
  if (showBkg) {
   setImageResource(R.drawable.alpha_pressed);
   alphaDrawable = getDrawable();

   alphaDrawable.setBounds(0, 0, getWidth(), getHeight());
  } else {
   setImageResource(R.drawable.alpha_normal);
   alphaDrawable = getDrawable();

   alphaDrawable.setBounds(0, 0, getWidth(), getHeight());
  }

  canvas.save();
  alphaDrawable.draw(canvas);
  canvas.restore();
 }

 @Override
 public boolean dispatchTouchEvent(MotionEvent event) {
  final float y = event.getY();
  final int oldChoose = choose;
  final int c = (int) (y / getHeight() * 28);

  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:
   showBkg = true;
   if (oldChoose != c && listener != null) {
    if (c >= 0 && c < ALPHAS.length) {
     listener.OnAlphaChanged(ALPHAS[c], c);
     choose = c;
    }
   }
   invalidate();
   break;

  case MotionEvent.ACTION_MOVE:
   if (oldChoose != c && listener != null) {
    if (c >= 0 && c < ALPHAS.length) {
     listener.OnAlphaChanged(ALPHAS[c], c);
     choose = c;
    }
   }
   invalidate();
   break;

  case MotionEvent.ACTION_UP:
   showBkg = false;
   choose = -1;
   invalidate();
   break;
  }
  return true;
 }

 // 設定事件
 public void setOnAlphaChangedListener(OnAlphaChangedListener listener) {
  this.listener = listener;
 }

 // 事件介面
 public interface OnAlphaChangedListener {
  public void OnAlphaChanged(String s, int index);
 }

}

 


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.