Android free to choose TextView text

Source: Internet
Author: User

Used EditText know, EditText has a feature, when the inside long press, will appear a ContextMenu, provides the choice of text, copy, cut and other functions. Sometimes, we will think, if do not appear this contextmenu, directly on the view of the choice of words, that much better ah. I believe a lot of people have this idea, unfortunately, so am I. So I took a look at the code for EditText and TextView and then solved the problem.

A lot of data on the internet said, to select a paragraph of text, only need to use Selection.getselectionstart () and Selection.getselectionend () to determine the selected text of the head and tail, and then add color on the line. It is nonsense, I dare say such code is not verified, on the Internet, and then a large number of people reprint each other, resulting in misleading a lot of people, cups ah!!

OK, let's analyze the solution.

TextView is the base class for many view, such as button, EditText are inherited from him, so edittext inside the code is very few. Let's take a look at EditText's source code, there is a override getdefaulteditable method, see the meaning of the name is editable, this method returns true directly. There is also a Getdefaultmovementmethod method, which returns the Arrowkeymovementmethod.getinstance (), by looking at the Arrowkeymovementmethod source, The basic determination of this method is to eject ContextMenu and trackball monitoring "culprit."

Below, we make a view to build our own edittext.

I named Textpage, inherited EditText, covered getdefaulteditable and getdefaultmovementmethod inside.

Java code

@Override   
Public Boolean getdefaulteditable () {
return false ;
}
@Override
Protected Movementmethod Getdefaultmovementmethod () {
return null;
}

Now test, found that the long press did not respond, the material is good, is the Getdefaultmovementmethod method control ContextMenu.

Look at the Arrowkeymovementmethod code, which provides KeyEvent, Trackball event ontrackballevent and Touch Event ontouchevent processing. Where are these events invoked? Let's look at the ontouchevent, Ontrackballevent, and Onkeyevent methods of TextView, and it's clear that these methods in Arrowkeymovementmethod are invoked in these event callbacks.

There's another question, where did the ContextMenu trigger? This problem, used ContextMenu know, view inside to use ContextMenu, need to cover a Oncreatecontextmenu method, and then create ContextMenu in the various options. In TextView find Oncreatecontextmenu, sure enough, inside the definition of selection, copy, paste and other options.

Now that we have found this, we can further analyze how the choices are made.

Oncreatecontextmenu just create a menu, so what does the menu trigger after it clicks? Oncreatecontextmenu inside defines a Menuhandler object, then passes as the parameter to Setonmenuitemclicklistener, finds Menuhandler, discovers inside the Onmenuitemclick returned the Ontextcontextmenuitem function, found ONTEXTCONTEXTMENUITEM,OMG, and finally found the function to click on the menu trigger. But there seems to be no key in it, and the chosen part is not here. Well, it should be in the events mentioned above.

The Ontouchevent method of Arrowkeymovementmethod is analyzed emphatically. Discovers an important method GetLayout (), and then gets a layout object that knows 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 then drag, and after releasing, the middle text will be selected, so beautiful!

Related Article

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.