Android development: freely select TextView text

Source: Internet
Author: User

We have introduced a series of Android development tutorials, such as how to implement TCP and UDP transmission,Build an Android Development Environment on MyEclipse 8.6Next, let's introduce the text of TextView in Android.

BKJIA recommended topics: Android development and application details

All those who have used EditText know that EditText has a feature. When the content is long pressed, A ContextMenu will appear, providing functions such as selecting text, copying, and cutting. If this ContextMenu is not displayed, you can directly select the text on the view, which is more convenient. So the author studied the EditText and TextView code and solved the problem.

Many documents on the Internet say that to select a text segment, you only need to use Selection. getSelectionStart () and Selection. getSelectionEnd () to determine the header and tail of the selected text, and then add the color. After tests, the author found that this result has misled many people and it is not feasible.

Let's analyze the solution.

TextView is a base class of many views. For example, buttons and EditText are inherited from them, so there are few codes in EditText. Let's take a look at the source code of EditText. There is an Override getDefaultEditable method to check whether the name is editable. This method returns true directly. There is also a getdefamovmovementmethod method, which returns ArrowKeyMovementMethod. getInstance (). By viewing the source code of ArrowKeyMovementMethod, it is basically determined that this method is the "culprit" of the ContextMenu and trackball listening ".

Next, we will make a view to build our own EditText.

My name is TextPage, which inherits EditText and overwrites getDefaultEditable and getdefamovmovementmethod.

Java code

 
 
  1. @Override    
  2. public boolean getDefaultEditable() {    
  3.     return false;    
  4. }    
  5. @Override    
  6. protected MovementMethod getDefaultMovementMethod() {    
  7.     return null;    
  8. }   

Now, I tested it and found that the long-pressed message did not respond. As expected, the getdefamovmovementmethod method controls the ContextMenu.

Take a look at the ArrowKeyMovementMethod code, which provides the processing of KeyEvent, trackball event onTrackballEvent, and touch event onTouchEvent. Where are these events called? Let's take a look at the onTouchEvent, onTrackballEvent, and onKeyEvent methods in TextView. In these event callbacks, these methods in ArrowKeyMovementMethod are called.

Another question is, where is the ContextMenu triggered? If you have used ContextMenu, you know that you need to use ContextMenu in view. You need to overwrite an onCreateContextMenu method, and then create various options of ContextMenu in view. Find onCreateContextMenu in TextView, which defines options such as selection, copy, and paste.

Now that we have found this, we can further analyze how the choice is achieved.

OnCreateContextMenu is only used to create a menu. What is triggered after the menu is clicked?

OnCreateContextMenu defines a MenuHandler object and passes it as a parameter to setOnMenuItemClickListener. Find MenuHandler and find onMenuItemClick in it to return the onTextContextMenuItem function. Find onTextContextMenuItem, OMG, finally, find the function triggered by clicking menu. However, there seems to be no key things in it, and the selected part is not here. In this case, it should be in the above mentioned events.

Focus on the onTouchEvent method of ArrowKeyMovementMethod. Find an important method getLayout (), then obtain a Layout object, and know the offset position of the current string through the x and y coordinates.

Then the problem can be solved perfectly. You can click anywhere and drag it. After the release, the text in the middle will be selected.

Java code

 
 
  1. Import android. content. Context;
  2. Import android. graphics. Color;
  3. Import android. text. Layout;
  4. Import android. text. Selection;
  5. Import android. view. ContextMenu;
  6. Import android. view. Gravity;
  7. Import android. view. MotionEvent;
  8. Import android. widget. EditText;
  9. /**
  10. * @ Author chroya
  11. */
  12. Public class TextPage extends EditText {
  13. Private int off; // The Offset Value of the string
  14. Public TextPage (Context context ){
  15. Super (context );
  16. Initialize ();
  17. }
  18. Private void initialize (){
  19. SetGravity (Gravity. TOP );
  20. SetBackgroundColor (Color. WHITE );
  21. }
  22. @ Override
  23. Protected void onCreateContextMenu (ContextMenu menu ){
  24. // No processing is performed. The context menu is displayed to prevent long presses.
  25. }
  26. @ Override
  27. Public boolean getDefaultEditable (){
  28. Return false;
  29. }
  30. @ Override
  31. Public boolean onTouchEvent (MotionEvent event ){
  32. Int action = event. getAction ();
  33. Layout layout = getLayout ();
  34. Int line = 0;
  35. Switch (action ){
  36. Case MotionEvent. ACTION_DOWN:
  37. Line = layout. getLineForVertical (getScrollY () + (int) event. getY ());
  38. Off = layout. getOffsetForHorizontal (line, (int) event. getX ());
  39. Selection. setSelection (getEditableText (), off );
  40. Break;
  41. Case MotionEvent. ACTION_MOVE:
  42. Case MotionEvent. ACTION_UP:
  43. Line = layout. getLineForVertical (getScrollY () + (int) event. getY ());
  44. Int curOff = layout. getOffsetForHorizontal (line, (int) event. getX ());
  45. Selection. setSelection (getEditableText (), off, curOff );
  46. Break;
  47. }
  48. Return true;
  49. }
  50. }

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.