Click is used: ClickableSpan method
Scrolling is activity_mai. XML added:
Android: maxLines="10"
Android: scrollbars="vertical"
These two properties,
The code is as follows:
Public class MainActivity extends AppCompatActivity {
TextView TextView;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView=(textView) the findViewById (R.i d.w word);
TextView. SetText (string2);
SpannableStringBuilder s=new SpannableStringBuilder (textView getText ());
TextView. SetText (s, textView. BufferType. SPANNABLE);
GetEachParagraph (textView);
TextView. SetMovementMethod (LinkMovementMethod getInstance ());
//textView setMovementMethod (ScrollingMovementMethod getInstance ());
}
//com.lowagie.text.paragraph
Public void getEachParagraph (TextView TextView) {
Spannable spans=(Spannable) textView. GetText ();
The Integer [] indices=getIndices (
TextView. GetText (). The toString (). The trim (), '\ n');
int start=0;
Int the end=0;
//recycle
//System. Out. Println (" length of indices: "+ indices. Length);
//System. Out. Println (" length of spans: "+ spans. Length ());
for (int i=0; I & lt;=indices. Length; I++) {
System. The out. Println (" indices index: "+ I);
//System. Out. Println (" the start of the beginning: "+ start);
ClickableSpan clickSpan=getClickableSpan (I);
//setSpan
End=(I & lt; Indices. Length? Indices: [I] spans. The length ());
Spans. SetSpan (clickSpan, start, end,
Spannable. SPAN_INCLUSIVE_EXCLUSIVE);
//System. Out. Println (" end of ending: "+ end);
Start=end + 1;
}
//change the selected text highlighted colors
//textView. SetHighlightColor (Color. ParseColor (" # 0 c83bd "));
}
//array
Public static Integer [] getIndices (String s, char c) {
Int pos=s.i ndexOf (c, 0);
ListIndices=new ArrayList (a);
While (pos!=1) {
Indices. The add (pos);
Pos=s.i ndexOf (c, pos + 1);
}
Return (Integer []) indices. ToArray (new Integer [0]).
}
//click
Private ClickableSpan getClickableSpan (final int I) {
Return new ClickableSpan () {
@ Override
Public void onClick (View widget) {
System. The out. Println (" I: "+ I);
TextView TV=(TextView widgets);
String s=TV
The getText ()
SubSequence (TV. GetSelectionStart (),
TV. GetSelectionEnd ()). The toString ();
The e (" onclick - : ", s);
}
@ Override
Public void updateDrawState TextPaint (ds) {
Ds. SetColor (Color BLACK);
Ds. SetUnderlineText (false);
}
};
}
}
CodePudding user response:
In front of, if you after setting ScrollingMovementMethod LinkMovementMethod can be replaced, which Span LinkMovementMethod will decide if you Click, then triggers the Click event, and ScrollingMovementMethod: no, you can write a support both the Movement of the class, the reference source assembly,Or don't ScrollingMovementMethod and sliding through the scrollview
CodePudding user response:
Slightly changetextView. SetMovementMethod (MyMovement getInstance ());
Package my. Study;
The import android. Text. Layout;
The import android. Text. NoCopySpan;
The import android. Text. Selection;
The import android. Text. Spannable;
The import android. Text. Method. LinkMovementMethod;
The import android. Text. Method. MovementMethod;
The import android. Text. Method. ScrollingMovementMethod;
The import android. Text. Style. ClickableSpan;
The import android. View. KeyEvent;
The import android. View. MotionEvent;
The import android. View. The view;
import android.widget.TextView;
Public class MyMovement extends ScrollingMovementMethod {
Private static final ints CLICK=1;
Private static final ints UP=2;
Private static final ints DOWN=3;
@ Override
Public Boolean canSelectArbitrarily () {
return true;
}
@ Override
Protected Boolean handleMovementKey (TextView widgets, Spannable buffer, int keyCode,
{int movementMetaState, KeyEvent event)
The switch (keyCode) {
Case KeyEvent. KEYCODE_DPAD_CENTER:
Case KeyEvent. KEYCODE_ENTER:
{if (KeyEvent. MetaStateHasNoModifiers (movementMetaState))
If (event. GetAction ()==KeyEvent. ACTION_DOWN & amp; &
Event. GetRepeatCount ()==0 & amp; & The action (CLICK, widgets, buffer)) {
return true;
}
}
break;
}
Return super. HandleMovementKey (widgets, buffer, keyCode, movementMetaState, event);
}
@ Override
Protected Boolean up (TextView widgets, Spannable buffer) {
If (action (UP, widgets, buffer)) {
return true;
}
Return super. Up (widgets, buffer);
}
@ Override
Protected Boolean down (TextView widgets, Spannable buffer) {
If (action (DOWN, widgets, buffer)) {
return true;
}
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull