Click the text to change the background color of the modified line. A dialog box is displayed, showing the background of the modified line.

Source: Internet
Author: User

Click the text to change the background color of the modified line. A dialog box is displayed, showing the background of the modified line.

 I want to reply to A comment by simply using one text. Click A row in the reply text column, and then the background color of the row changes and A dialog box is displayed. For example, we want to reply to, click the row where A is located (A replies B: hahaha ).

Note that the following two lines contain the reply text in a textView.

I have been searching the Internet for a long time, and I have finally realized it with some of my own knowledge.

 

Reveal the answer (steps are as follows ):

/***

For the second line

CommentName is

ReplyName is B,

CommentContent is the content

***/

1.

SpannableString commentReplyNameContent = null;

CommentReplyNameContent = new SpannableString (commentName + "reply" + replyName + ":" + commentContent );

CommentReplyNameContent. setSpan (new ClickableSpan () // set the effect of the click (only the keyboard is displayed here) {@ Override
Public void updateDrawState (TextPaint ds)
{
Super. updateDrawState (ds );
Ds. setUnderlineText (false); // cancel the underline.
} @ Override
Public void onClick (View widget)
{
ShowKeyBoard (comment. getComentName (); // the keyboard is displayed.
// For other operations, comment. getComentName () is the name of the person to be replied to. If you click these two lines, comment. getComentName () is.
}
}, 0, commentName. length () + replyName. length () + commentContent. length () + 3, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); commentReplyNameContent. setSpan // set the Color of A (new ForegroundColorSpan (Color. BLUE), 0, commentName. length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); commentReplyNameContent. setSpan // set the background of the entire row to Black (new ForegroundColorSpan (Color. BLACK), commentName. length (), commentName. length () + replyName. length () + commentContent. length () + 3, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE);/*** replyContent for this textView ***/replyContent. append ("\ n"); // line feed
ReplyContent. append (commentReplyNameContent); // append this row to the end of the textView: replyContent. setMovementMethod (new MyLinkMovementMethod (); // This sentence is used to respond to the click effect.

 

2. Click to change the background color of the line and override LinkMovementMethod.

Private class MyLinkMovementMethod extends LinkMovementMethod
{
@ Override
Public boolean onTouchEvent (TextView widget, Spannable buffer, MotionEvent event)
{
Int action = event. getAction ();
If (action = MotionEvent. ACTION_DOWN | action = MotionEvent. ACTION_DOWN
| Action = MotionEvent. ACTION_MOVE)
{
Int x = (int) event. getX ();
Int y = (int) event. getY (); x-= widget. getTotalPaddingLeft (); // return the textView offset. If textView is set to padding = 3px, 3 is returned.
Y-= widget. getTotalPaddingTop ();/**
* The left side of the view exceeds the screen offset. If a view slides between the left and right, the view on the left is blocked because the view is large,
* The blocked offset is getScrollX (). Similarly, getScrollY ()
*/
X + = widget. getScrollX ();
Y + = widget. getScrollY (); // the above aims to locate the absolute coordinates of the click position in the entire view component (not the relative coordinates of the screen)
Layout layout = widget. getLayout ();
Int line = layout. getLineForVertical (y); // gets the number of lines of text at the click position.
Int off = layout. getOffsetForHorizontal (line, x); // get the offset of the click position. // get the span through the offset (determined by the span corresponding to the offset)
ClickableSpan [] link = buffer. getSpans (off, off, ClickableSpan. class );
If (link. length! = 0)
{
If (action = MotionEvent. ACTION_UP)
{
Link [0]. onClick (widget); buffer. setSpan (new BackgroundColorSpan (Color. TRANSPARENT), buffer. getSpanStart (link [0]),
Buffer. getSpanEnd (link [0]), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Selection. removeSelection (buffer );}
Else if (action = MotionEvent. ACTION_DOWN)
{Buffer. setSpan (new BackgroundColorSpan (Color. GRAY), buffer. getSpanStart (link [0]),
Buffer. getSpanEnd (link [0]), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Selection. setSelection (buffer, buffer. getSpanStart (link [0]), buffer. getSpanEnd (link [0]);
}
Else if (action = MotionEvent. ACTION_MOVE)
{Buffer. setSpan (new BackgroundColorSpan (Color. TRANSPARENT), buffer. getSpanStart (link [0]),
Buffer. getSpanEnd (link [0]), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); Selection. removeSelection (buffer );
} Return true;
}
Else
{
Selection. removeSelection (buffer );
}
}
// Returnsuper. onTouchEvent (widget, buffer, event) is required );
// Otherwise, because onTouchEvent is overwritten, ClickableSpan cannot be called during action_up.
Return super. onTouchEvent (widget, buffer, event );
}
}

3. Pop-up keyboard

 

/*** Pop-up keyboard */private void showKeyBoard (String name) {commentEdit. setHint ("reply" + name); // The input box shows who will reply to commentEdit. requestFocus (); (new Handler ()). postDelayed (new Runnable () {public void run () {(InputMethodManager) (commentEdit. getContext (). getSystemService (Context. INPUT_METHOD_SERVICE ))). toggleSoftInput (0, InputMethodManager. HIDE_NOT_ALWAYS); }}, 100 );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.