Android edittext cursor Positioning

Source: Internet
Author: User
  • Selection is a tool class. It is a tool class for range selection. its own methods are static. I tested all the methods and stayed up late again. the test is performed in an ontouchevent method that inherits the edittext subclass. this is a summary.
  • The real parameter of text is geteditabletext (), and the real parameter of layout is getlayout ()
  • Method of the selected area:
  • Selection. extenddown (spannable text, layout); // coordinates of the current cursor, selected to the (X axis) Position of the next row

1 2 3 4 cursor 5 6 7 8 9

1 2 3 4 5 6 7 8 9

  • Selection. extendup (spannable text, layout); // coordinates of the current cursor, selected to the (X axis) Position of the previous line

1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9

  • Selection. extendleft (spannable text, layout); // coordinates of the current cursor, selected to the position of the X-1

1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9

  • Selection. extendright (spannable text, layout); // coordinates of the current cursor, selected to the position x + 1

1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9

  • Selection. extendtoleftedge (spannable text, layout); // coordinates of the current cursor, selected to the leftmost position of the same row

1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9

  • Selection. extendtorightedge (spannable text, layout); // coordinates of the current cursor, which is selected to the rightmost position of the same row

1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9

  • Jump cursor method:
  • Selection. movedown (spannable text, layout); // The coordinates of the current cursor to jump to the position of the next line (X axis)

1 2 3 4 Original cursor 5 6 7 8 9

1 2 3 4 new optical mark 5 6 7 8 9

  • There are also moveup, moveleft, and movetoleftedge, which should be explained in conjunction with other methods above! This is omitted.

 

 

  • Select and cancel all methods at the specified position:
  • Selection. setselection (spannable text, int index); // coordinates of the current cursor to jump to the index (index starts from 0), note that it is not moveselection.

For example, selection. setselection (geteditabletext (), 5 );

1 2 3 4 Original cursor 5 New Light mark 6 7 8 9

1 2 3 4 5 6 7 8 9

  • Selection. extendselection (spannable text, int index); // coordinates of the current cursor, selected to the position where the index is index (index starts from 0)

For example, selection. extendselection (geteditabletext (), 5 );

1 2 3 4 Original cursor 5 6 7 8 9

1 2 3 4 5 6 7 8 9

  • Selection. setselection (spannable text, int start, int stop) // select the area from index start to index stop

For example, selection. setselection (geteditabletext (), 5, 9 );

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

  • Selection. selectall (spannable text); // select all

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

  • Selection. removeselection (spannable text); // deselect the selected state and reset the cursor coordinates to the beginning.

1 2 3 4 5 6 7 8 9 ==> cursor 1 2 3 4 5 6 7 8 9

1 2 3 4 cursor 5 6 7 8 9 1 2 3 4 5 6 7 8 9

 

 

  • Int selection. getselectionend (charsequence text)// The Real Text parameter is geteditabletext ()
  1. If no selected area exists and a cursor exists, the index of the cursor is returned;
  2. If a selected region exists, the end index of the returned region is returned;
  3. If no area or cursor is selected,-1 is returned.
  • Int selection. getselectionstart (charsequence text)// The Real Text parameter is geteditabletext ()
  1. If no selected area or cursor exists, the index of the cursor is returned;
  2. If a selected region exists, the start index of the returned region is returned;
  3. If no area or cursor is selected,-1 is returned.

The two methods also have edittext. How can I obtain the selected text? The two methods depend on edittext. Take a start index start and end index to determine whether they are the same. If they are different, the text is selected,Selected text = medittext. gettext (). Subsequence (START, end );

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.