The location of the Android input box cursor and hint
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p><p><p>, to achieve this demand, the general layout of the way is the left a button, the right button, the middle of a edittext, in order to input the response range of the box is larger often does not set the width of wrap_content, or set to match_parent/ Fill_parent either given a minwidth+wrap_content.</p></p><p><p>Regardless of layout, gravity or layout_gravity should be center to meet the requirements Shown. however, the problem is, if the gravity set up for the center, unfortunately, most of the mobile phone (the author is actually automatically correct the cursor and the relative position of Hint) the actual effect is that the cursor will be located in the middle of hint, to know that the gravity will affect the location of the cursor, Gravity is left cursor on the leftmost, graity is right cursor is at the rightmost of hint, of course gravity is center cursor naturally in the middle of hint.</p></p><p><p></p></p><p><p>Borrow a picture (from the almighty stackoverflow, but the almighty SOF also did not give an answer)</p></p><p><p></p></p><p><p>Thus pondering the various methods of salvation, there are mainly 3 solutions two angles to Solve. The perfect angle is to try to adjust the position of the cursor, the next way is to show hint (that is, the input box does not have Text) when the cursor is Hidden. There are two scenario trigger points, one for the first time without any text but set hint at this point, the entry should be the Onfocuschange method, and the other is the text is cleared during the dynamic input process, and the entry is in Ontextchange.</p></p><p><p>I have summed up three countermeasures as Follows.</p></p><p><p>The worst: when the need to display hint hint set to an empty string, do not need to display hint when the hint revert to the need to set the String. The effect of this method is that the interface opens to see hint, when you click on the input box to start typing there is no hint, although the cursor is visible, but there is a flaw, and in the Onfocuschange method set hint will cause the key points two times will play the input method ( Specific reasons have not yet been delved into).</p></p><p><p>Strategy: hide the cursor when the hint is needed, once the user starts to enter the display Cursor. This effect is similar to the first, an open interface can see hint, but do not see the cursor, you have to start the input to See. This is also flawed, when the user points the input box (get the user focus) no cursor user will be very depressed do not know can input, lack of cursor guidance Role.</p></p><p><p>The best policy: that is to adjust the position, in fact, it is not said to adjust the position of the cursor, but the reverse thinking of the position of the input box to apply the position of the cursor needs to be Placed. This kind of solution is a bit complicated and has a large amount of change. First of all you edittextd width can not be specified, minwidth cannot be set by the input box and to set the minimum width in its parent container, so its width is preferably wrap_content, and set a certain padding to retain a certain user touch response Area. Then EditText gravity or center, need to adjust is its layoutgravity, when the user has input undoubtedly need to center, when there is no input need to display hint, it will be set to left, It then calculates the need to set the MarginLeft distance. This distance is EditText (width of the parent container width-hint)/2, of course, to get layoutwdth need some skill, to get the width of hint more need a bit of skill. This allows the cursor to be located in front of the hint, the curve to the National Salvation Bar.</p></p><p><p>The dry goods are coming.</p></p><p><p>Android to get wide high often can get 0 this value, the general experience will be in the Onwindowsfocus to get, and then the number of pixels in the string, you need to use your display hint brush to measure the string, because you need to know the font properties, such as font, size, weight, etc.</p></p><p><p>Directly on the code slightly:</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onwindowfocuschanged (<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Haswindowfocus) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. onwindowfocuschanged (haswindowfocus); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mspacewidth <= 0) mspacewidth = getwidth ()-left.getwidth ()-<span style="color: #000000;"><span style="color: #000000;">Right.getwidth (); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onfocuschange (View v,<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Hasfocus) {adjustcursorposition (etinput.gettext ()); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">aftertextchanged (Editable s) {adjustcursorposition (etinput.gettext ()); }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">adjustcursorposition (charsequence Text) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(! Textutils.isempty (etinput.gethint ())) {adjustgravityforcursor (text);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adjustcursorvisible (text);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">adjusthintcontent (hasfocus (), text);</span></span><span style="color: #000000;"><span style="color: #000000;"> } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">adjustgravityforcursor (charsequence Text) {layoutparams LP</span></span>=<span style="color: #000000;"><span style="color: #000000;">(layoutparams) Etinput.getlayoutparams (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(mspacewidth <= 0) mspacewidth = Lp.width-left.getwidth ()-<span style="color: #000000;"><span style="color: #000000;">Right.getwidth (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">Textutils.isempty (text)) {etinput.setgravity (gravity.center); Lp.leftmargin</span></span>= 0<span style="color: #000000;"><span style="color: #000000;">; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{etinput.setgravity (gravity.left); Lp.leftmargin</span></span>= (mspacewidth-measuretext (ETINPUT,MHINT))/2<span style="color: #000000;"><span style="color: #000000;">; } etinput.setlayoutparams (lp); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">measuretext (TextView TextView, String Text) {textpaint paint</span></span>=<span style="color: #000000;"><span style="color: #000000;">Textview.getpaint (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">) Paint.measuretext (text); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">adjustcursorvisible (charsequence Text) {etinput.setcursorvisible (</span></span>!<span style="color: #000000;"><span style="color: #000000;">Textutils.isempty (text)); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Adjusthintcontent (<span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">hasfocus, charsequence Text) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(textutils.isempty (text)) {etinput.sethint (hasfocus</span></span>? ""<span style="color: #000000;"><span style="color: #000000;">: mhint); } }</span></span></pre><p><p></p></p><p><p>The location of the Android input box cursor and hint</p></p></span>
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