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
- @ Override
- Public
Boolean
Getdefaulteditable (){
- Return
False
;
- }
- @ Override
- Protected
Movementmethod getdefaultmovementmethod (){
- Return
Null
;
- }
@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
- Import
Android. content. context;
- Import
Android. Graphics. color;
- Import
Android. Text. layout;
- Import
Android. Text. selection;
- Import
Android. View. contextmenu;
- Import
Android. View. gravity;
- Import
Android. View. motionevent;
- Import
Android. widget. edittext;
- /**
- * @ Author chroya
- */
- Public
Class
Textpage
Extends
Edittext {
- Private
Int
Off;
// Offset value of the string
- Public
Textpage (context ){
- Super
(Context );
- Initialize ();
- }
- Private
Void
Initialize (){
- Setgravity (gravity. Top );
- Setbackgroundcolor (color. White );
- }
- @ Override
- Protected
Void
Oncreatecontextmenu (contextmenu menu ){
- // No processing is performed. The context menu is displayed to prevent long presses.
- }
- @ Override
- Public
Boolean
Getdefaulteditable (){
- Return
False
;
- }
- @ Override
- Public
Boolean
Ontouchevent (motionevent event ){
- Int
Action = event. getaction ();
- Layout layout = getlayout ();
- Int
Line =
0
;
- Switch
(Action ){
- Case
Motionevent. action_down:
- Line = layout. getlineforvertical (getscrolly () + (int
) Event. Gety ());
- Off = layout. getoffsetforhorizontal (line, (int
) Event. getx ());
- Selection. setselection (geteditabletext (), off );
- Break
;
- Case
Motionevent. action_move:
- Case
Motionevent. action_up:
- Line = layout. getlineforvertical (getscrolly () + (int
) Event. Gety ());
- Int
Curoff = layout. getoffsetforhorizontal (line ,(
Int
) Event. getx ());
- Selection. setselection (geteditabletext (), off, curoff );
- Break
;
- }
- Return
True
;
- }
- }