Android settings EditText cursor position

Source: Internet
Author: User

There are many editable pop-up boxes in Android, some of which let us modify the characters, where is the cursor position positioned?just solved a bug is about the position of the cursor, it seems that Android native in this case is to position the cursor to the front of the string. The requirement is to position the cursor to the last face of the character. The modification is textview this control, because EditText also inherits the TextView. Among the SetText methods are:

1 private void SetText (charsequence text, Buffertype type,
2 boolean notifybefore, int oldlen) { 
3 ...
4 if (text instanceof spannable) {
5 Spannable sp = (spannable) text;
6
7 ...
8 if (mmovement! = null) {
9 Mmovement.initialize (this, (spannable) text);
10//text is not editable.
if (this instanceof Editable)
12//Set cursor position
selection.setselection ((spannable) text, text.length ());
-
.....
+ }

As you can see from the red code, Google wants the cursor to be at the end of the default text, but log discovery (this instanceof Editable) is not true, which means selection.setselection ((spannable) Text, Text.length ()), and will not be executed.

1 log.d ("TextView", "(type = = buffertype.editable) =" + (type = = buffertype.editable));
2 if (type = = buffertype.editable) {
3 LOG.D ("TextView", "Format text". Set cursor to the end ");
4 Selection.setselection ((spannable) text, text.length ());
5}

This kind of modification can be done.

in writing the application, if we want to position the cursor to a location, you can use the following method:

1 charsequence Text = Edittext.gettext ();
2//debug.asserts (text instanceof spannable);
3 if (text instanceof spannable) {
4 spannable Spantext = (spannable) text;
5 selection.setselection (Spantext, Text.length ());
6}

where the Red tag code is the position you want to set, here is the setting to the end of the text.

Android settings EditText cursor position

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.