First view results
First-letter prompt 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; // whether to display the background
Private int choose; // The Position of the selected first letter
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] = ""; // "" indicates search
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;
}
// Set the event
Public void setOnAlphaChangedListener (OnAlphaChangedListener listener ){
This. listener = listener;
}
// Event Interface
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; // whether to display the background
Private int choose; // The Position of the selected first letter
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] = ""; // "" indicates search
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;
}
// Set the event
Public void setOnAlphaChangedListener (OnAlphaChangedListener listener ){
This. listener = listener;
}
// Event Interface
Public interface OnAlphaChangedListener {
Public void OnAlphaChanged (String s, int index );
}
}