17
17
import android .text .TextWatcher ;
18
18
import android .text .method .ArrowKeyMovementMethod ;
19
19
import android .util .AttributeSet ;
20
- import android .util .Log ;
21
20
import android .util .TypedValue ;
22
21
import android .view .Gravity ;
23
22
import android .view .KeyEvent ;
@@ -77,24 +76,34 @@ public class TagGroup extends ViewGroup {
77
76
78
77
/** The tag outline border color. */
79
78
private int borderColor ;
79
+
80
80
/** The tag text color. */
81
81
private int textColor ;
82
+
82
83
/** The tag background color. */
83
84
private int backgroundColor ;
85
+
84
86
/** The dash outline border color. */
85
87
private int dashBorderColor ;
88
+
86
89
/** The input tag hint text color. */
87
90
private int inputHintColor ;
91
+
88
92
/** The input tag type text color. */
89
93
private int inputTextColor ;
94
+
90
95
/** The checked tag outline border color. */
91
96
private int checkedBorderColor ;
97
+
92
98
/** The check text color */
93
99
private int checkedTextColor ;
94
- /** The checked markder color. */
100
+
101
+ /** The checked marker color. */
95
102
private int checkedMarkerColor ;
103
+
96
104
/** The checked tag background color. */
97
105
private int checkedBackgroundColor ;
106
+
98
107
/** The tag background color, when the tag is being pressed. */
99
108
private int pressedBackgroundColor ;
100
109
@@ -119,8 +128,11 @@ public class TagGroup extends ViewGroup {
119
128
/** Listener used to dispatch tag change event. */
120
129
private OnTagChangeListener mOnTagChangeListener ;
121
130
131
+ /** Listener used to dispatch tag click event. */
132
+ private OnTagClickListener mOnTagClickListener ;
133
+
122
134
/** Listener used to handle tag click event. */
123
- private OnTagClickListener mOnTagClickListener = new OnTagClickListener ();
135
+ private InternalTagClickListener mInternalTagClickListener = new InternalTagClickListener ();
124
136
125
137
public TagGroup (Context context ) {
126
138
this (context , null );
@@ -464,7 +476,7 @@ protected void appendInputTag(String tag) {
464
476
}
465
477
466
478
final TagView newInputTag = new TagView (getContext (), TagView .STATE_INPUT , tag );
467
- newInputTag .setOnClickListener (mOnTagClickListener );
479
+ newInputTag .setOnClickListener (mInternalTagClickListener );
468
480
addView (newInputTag );
469
481
}
470
482
@@ -475,7 +487,7 @@ protected void appendInputTag(String tag) {
475
487
*/
476
488
protected void appendTag (CharSequence tag ) {
477
489
final TagView newTag = new TagView (getContext (), TagView .STATE_NORMAL , tag );
478
- newTag .setOnClickListener (mOnTagClickListener );
490
+ newTag .setOnClickListener (mInternalTagClickListener );
479
491
addView (newTag );
480
492
}
481
493
@@ -513,6 +525,27 @@ public interface OnTagChangeListener {
513
525
void onDelete (TagGroup tagGroup , String tag );
514
526
}
515
527
528
+ /**
529
+ * Register a callback to be invoked when a tag is clicked.
530
+ *
531
+ * @param l the callback that will run.
532
+ */
533
+ public void setOnTagClickListener (OnTagClickListener l ) {
534
+ mOnTagClickListener = l ;
535
+ }
536
+
537
+ /**
538
+ * Interface definition for a callback to be invoked when a tag is clicked.
539
+ */
540
+ public interface OnTagClickListener {
541
+ /**
542
+ * Called when a tag has been clicked.
543
+ *
544
+ * @param tag The tag text of the tag that was clicked.
545
+ */
546
+ void onTagClick (String tag );
547
+ }
548
+
516
549
/**
517
550
* Per-child layout information for layouts.
518
551
*/
@@ -570,9 +603,9 @@ public void writeToParcel(Parcel dest, int flags) {
570
603
}
571
604
572
605
/**
573
- * The tag view click listener.
606
+ * The tag view click listener for internal use .
574
607
*/
575
- class OnTagClickListener implements OnClickListener {
608
+ class InternalTagClickListener implements OnClickListener {
576
609
@ Override
577
610
public void onClick (View v ) {
578
611
final TagView tag = (TagView ) v ;
@@ -598,8 +631,9 @@ public void onClick(View v) {
598
631
}
599
632
}
600
633
} else {
601
- // TODO Dispatch tag click event.
602
- Log .i ("TagGroup" , "Tag " + tag .getText () + " clicked." );
634
+ if (mOnTagClickListener != null ) {
635
+ mOnTagClickListener .onTagClick (tag .getText ().toString ());
636
+ }
603
637
}
604
638
}
605
639
}
@@ -669,7 +703,7 @@ class TagView extends TextView {
669
703
/** The path effect provide draw the dash border. */
670
704
private PathEffect mPathEffect = new DashPathEffect (new float []{10 , 5 }, 0 );
671
705
672
- public TagView (Context context , int state , CharSequence text ) {
706
+ public TagView (Context context , final int state , CharSequence text ) {
673
707
super (context );
674
708
setPadding (horizontalPadding , verticalPadding , horizontalPadding , verticalPadding );
675
709
setLayoutParams (new TagGroup .LayoutParams (TagGroup .LayoutParams .WRAP_CONTENT ,
@@ -687,6 +721,13 @@ public TagView(Context context, int state, CharSequence text) {
687
721
setHint (state == STATE_INPUT ? inputHint : null );
688
722
setMovementMethod (state == STATE_INPUT ? ArrowKeyMovementMethod .getInstance () : null );
689
723
724
+ setOnLongClickListener (new OnLongClickListener () {
725
+ @ Override
726
+ public boolean onLongClick (View v ) {
727
+ return state != STATE_INPUT ;
728
+ }
729
+ });
730
+
690
731
if (state == STATE_INPUT ) {
691
732
requestFocus ();
692
733
0 commit comments