Android freely selects textview text

Source: Internet
Author: User

All those who have used edittext know that edittext has a special feature.
A contextmenu is displayed, which provides functions such as selecting text, copying, and cutting. Sometimes, we will think that if this contextmenu is not displayed, it will be directly in the view
Select text on. I believe many people have such ideas. Unfortunately, me too. So I studied the edittext and textview code and solved the problem.
.

Many documents on the Internet say that to select a paragraph of text, you only need to use selection. getselectionstart () and
Selection. getselectionend () determines the header and end of the selected text, and then adds the color. It's a nonsense. I dare say such code is not verified at all,
I sent it to the Internet, and then a large number of people reprinted each other. As a result, many people were misled,Cup
Ah !!

Okay. 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.
Source code of edittext. There is an override getdefaulteditable method to check whether the name is editable. This method returns true directly. Also
There is a getdefamovmovementmethod method that returns arrowkeymovementmethod. getinstance (),
By viewing the source code of arrowkeymovementmethod, we can basically determine that this method is the "culprit" of the contextmenu and trackball monitoring ".
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. }
    @Override    public boolean getDefaultEditable() {        return false;    }    @Override    protected MovementMethod getDefaultMovementMethod() {        return null;    }

 

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

The arrowkeymovementmethod Code provides the keyevent, trackball event ontrackballevent, and touch events.
Ontouchevent processing. Where are these events called? Let's take a look at the ontouchevent, ontrackballevent, and
The onkeyevent method can be used to call these methods in arrowkeymovementmethod.

Another question is, where is the contextmenu triggered? This problem is known to anyone who has used contextmenu. To use contextmenu in view, Overwrite
An oncreatecontextmenu method, and then create various options of contextmenu in it. Find in textview
Oncreatecontextmenu, which defines options such as selection, copying, and pasting.
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
The menuhandler object is then passed as a parameter to setonmenuitemclicklistener, locate menuhandler, and find
Onmenuitemclick returns the ontextcontextmenuitem function, locate ontextcontextmenuitem, OMG, and final
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, so beauul ul!

 

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;
    // Offset value of the string

  14. Public
    Textpage (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.