Character size and cursor position in Android EditText

Source: Internet
Author: User

Recent work requires you to write a numeric keypad for the input of numbers, by customizing a Dialog and then displaying the numbers in the EditText. Encountered in the process encountered a problem, in the EditText click event to shield the system keyboard and bounce out of the defined dialog, unable to get the user selected characters that selection, the problem is that the user can not select a location in the middle to modify the input value.

To solve this problem, you need to get selection in the EditText click event.

The solution is: 1, in EditText click on the event to get the user click Position, the main take the Touchx value (here mainly discusses the text direction for the horizontal situation);

2. Get the inner margin paddingleft value of the EditText (including left inner margin and right inner margin, where only left alignment is discussed);

3. Get the text size textsize (this is worth the size and the units set in the XML file, it is best to use the SP.) The value obtained by using the GetTextSize method is the value that has been weighted by the system font size, but not the actual font size that is displayed, and the actual font size should be multiplied by this value by Textscalex. By default, the unit is PX, other cases can use another method of GetTextSize overload, return the corresponding value according to the unit;

4, Get EditText Textscalex (this value is used to calculate the actual size of the font display);

After you get the above value, you can calculate the actual size of the character. Here is a description: Chinese characters, numbers, capital letters, lowercase letters display size is different, where the Chinese character size is Textsize*textscalex, the number size is TEXTSIZE*TEXTSCALEX/2, the letter size is related to the specific letter. Only numbers are used here for discussion.

Numeric size calculation formula: Numsize = textsize*textscalex/2*1.1;//here multiplied by 1.1 to add the distance between the numbers, by the actual measurement, the interval between characters is 0.1 times times the size of the character

Cursor position calculation: selection = (int) ((touchx-paddingleft)/(numsize));//Direct rounding here, you can position the cursor before the first character

You should also check whether the selection exceeds the actual character length in the EditText or less than 0 and handles it accordingly.

Character size and cursor position in Android EditText

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.