11package com .neo .highlightproject ;
22
3+ import android .content .Intent ;
34import android .graphics .Color ;
5+ import android .net .Uri ;
46import android .os .Bundle ;
57import android .os .Handler ;
68import android .os .Looper ;
9+ import android .text .method .LinkMovementMethod ;
710import android .text .style .StrikethroughSpan ;
811import android .text .style .UnderlineSpan ;
12+ import android .widget .Toast ;
913
14+ import androidx .annotation .NonNull ;
1015import androidx .appcompat .app .AppCompatActivity ;
1116
1217import com .neo .highlight .core .Highlight ;
1318import com .neo .highlight .core .Scheme ;
1419import com .neo .highlight .util .listener .HighlightTextWatcher ;
1520import com .neo .highlight .util .scheme .ColorScheme ;
21+ import com .neo .highlight .util .scheme .LinkScheme ;
22+ import com .neo .highlight .util .scheme .OnClickScheme ;
1623import com .neo .highlight .util .scheme .StyleScheme ;
1724import com .neo .highlightproject .databinding .ActivityMainBinding ;
1825
@@ -38,21 +45,21 @@ private void configToolbar() {
3845
3946 highlight .addScheme (
4047 new StyleScheme (
41- "Highlight" ,
48+ Pattern . compile ( "Highlight" ) ,
4249 StyleScheme .STYLE .BOLD_ITALIC
4350 )
4451 );
4552
4653 highlight .addScheme (
4754 new ColorScheme (
48- "light" ,
55+ Pattern . compile ( "light" ) ,
4956 Color .parseColor ("#FF03DAC5" )
5057 )
5158 );
5259
5360 highlight .addScheme (
5461 new ColorScheme (
55- "Project" ,
62+ Pattern . compile ( "Project" ) ,
5663 Color .BLACK
5764 )
5865 );
@@ -68,35 +75,35 @@ private void initHighlightExample() {
6875
6976 highlightTextWatcher .addScheme (
7077 new ColorScheme (
71- "\\ b(J|j )ava\\ b" ,
78+ Pattern . compile ( "\\ b([Jj] )ava\\ b" ) ,
7279 Color .parseColor ("#FC0400" )
7380 )
7481 );
7582
7683 highlightTextWatcher .addScheme (
7784 new ColorScheme (
78- "\\ b(K|k )otlin\\ b" ,
85+ Pattern . compile ( "\\ b([Kk] )otlin\\ b" ) ,
7986 Color .parseColor ("#FC8500" )
8087 )
8188 );
8289
8390 highlightTextWatcher .addScheme (
8491 new ColorScheme (
85- "\\ b(J|j )ava(S|s )cript\\ b" ,
92+ Pattern . compile ( "\\ b([Jj] )ava([Ss] )cript\\ b" ) ,
8693 Color .parseColor ("#F5E200" )
8794 )
8895 );
8996
9097 highlightTextWatcher .addScheme (
9198 new ColorScheme (
92- "\\ b(A|a )ndroid\\ b" ,
99+ Pattern . compile ( "\\ b([Aa] )ndroid\\ b" ) ,
93100 Color .parseColor ("#00CA0E" )
94101 )
95102 );
96103
97104 highlightTextWatcher .addScheme (
98105 new StyleScheme (
99- "\\ b([Hh])ighlight\\ b" ,
106+ Pattern . compile ( "\\ b([Hh])ighlight\\ b" ) ,
100107 StyleScheme .STYLE .BOLD_ITALIC
101108 )
102109 );
@@ -113,9 +120,14 @@ public Pattern getRegex() {
113120 }
114121
115122 @ Override
116- public Object getSpan () {
123+ public Object getSpan (@ NonNull CharSequence text ) {
117124 return new StrikethroughSpan ();
118125 }
126+
127+ @ Override
128+ public boolean getClearOldSpan () {
129+ return false ;
130+ }
119131 }
120132 );
121133
@@ -131,33 +143,74 @@ public Pattern getRegex() {
131143 }
132144
133145 @ Override
134- public Object getSpan () {
146+ public Object getSpan (@ NonNull CharSequence text ) {
135147 return new UnderlineSpan ();
136148 }
149+
150+ @ Override
151+ public boolean getClearOldSpan () {
152+ return false ;
153+ }
137154 }
138155 );
139156
140157 highlightTextWatcher .addSpanType (StrikethroughSpan .class );
141158
159+ //add link scheme
160+
161+ highlightTextWatcher .addScheme (
162+ new LinkScheme ().setClearOldSpan (true )
163+ );
164+
165+ binding .edittext .setMovementMethod (LinkMovementMethod .getInstance ());
166+
167+ //add click scheme
168+
169+ highlightTextWatcher .addScheme (
170+ new OnClickScheme (Pattern .compile ("[hH]ighlight" ), (CharSequence text ) -> showToast ())
171+ );
172+
173+ highlightTextWatcher .addScheme (
174+ new OnClickScheme (Pattern .compile ("Irineu A\\ . Silva" ), (CharSequence text ) ->
175+ goToMyGithub ()
176+ ).setPainTextColor (Color .GRAY )
177+ );
178+
142179 binding .edittext .addTextChangedListener (highlightTextWatcher );
143180
144181 //binding.edittext.setText(R.string.example);
145182 initAutoText (getString (R .string .example ));
146183 }
147184
185+ private void goToMyGithub () {
186+ String url = "https://github.com/Irineu333" ;
187+ Intent i = new Intent (Intent .ACTION_VIEW );
188+ i .setData (Uri .parse (url ));
189+ startActivity (i );
190+ }
191+
192+ private void showToast () {
193+ Toast .makeText (
194+ MainActivity .this ,
195+ "Highlight is the best!!" ,
196+ Toast .LENGTH_SHORT
197+ ).show ();
198+ }
199+
148200 private void initAutoText (String text ) {
149201
150202 binding .edittext .setText ("" );
203+ Handler handler = new Handler (Looper .getMainLooper ());
151204
152205 new Thread (() -> {
153206 try {
154- for (int index = 0 ; index < text .length (); index ++ ) {
207+ for (char charToAdd : text .toCharArray () ) {
155208
156209 Thread .sleep (50 );
157- char charToAdd = text .charAt (index );
158210
159- new Handler (Looper .getMainLooper ())
160- .post (() -> binding .edittext .getText ().append (charToAdd ));
211+ handler .post (
212+ () -> binding .edittext .getText ().append (charToAdd )
213+ );
161214 }
162215 } catch (InterruptedException e ) {
163216 e .printStackTrace ();
0 commit comments