Android Official Development Document Training Series Course Chinese version: keyboard input Processing control Input Method display mode

Source: Internet
Author: User
<span id="Label3"></p><p><p>Original Address: http://android.xsoftlab.net/training/keyboard-input/visibility.html</p></p><p><p>When the input focus enters or leaves the text box, Android displays or hides the input method as Appropriate. The system also determines how the UI and text boxes appear above the input method. For example, when the free space in the vertical direction is very tense, the text box may fill the entire area above the input method. For most apps, This default behavior is what they need.</p></p><p><p>In other cases, however, you may need to directly control how the input method is displayed and how the UI will be arranged when the input method is Visible. So this lesson is mainly about how to achieve this.</p></p>Show Input method when activity is started<p><p>Although Android will focus on the first text box when the activity starts, it does not trigger the input method Popup. This behavior is consistent with normal habits, because entering a text box may not be the first task after the activity Starts. In any case, if entering a text box is the first task of the activity (such as the login screen), then you may want to enter the activity by default and the input method will pop up.</p></p><p><p>In order to display the input method after the activity is started, you need to add the attribute android:windowsoftinputmode to the corresponding activity element in the manifest File. As follows:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-tag"><<span class="hljs-title">application</span> <span class="hljs-attribute">...</span> ></span> <span class="hljs-tag"><<span class="hljs-title">activity</span> <span class="hljs-attribute">android:windowSoftInputMode</span>=<span class="hljs-value">"stateVisible"</span> <span class="hljs-attribute">...</span> ></span> ... <span class="hljs-tag"></<span class="hljs-title">activity</span>></span> ...<span class="hljs-tag"></<span class="hljs-title">application</span>></span></code></pre></pre> <blockquote> <blockquote> <p><strong>note:</strong> If the User's device contains an entity key, the soft keyboard will not pop up.</p> </blockquote> </blockquote>Pop-up input by demand<p><p>If there is a method in the Activity's life cycle: you want to make sure that the IME is visible after the method call, then you can use Inputmethodmanager to eject it.</p></p><p><p>For example, The following method holds a view object that the user will enter in this view, so the call to the Requestfocus () method can assign the focus to it, and then Showsoftinput () will open the input method:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-java hljs "><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">showSoftKeyboard</span>(View view) { <span class="hljs-keyword">if</span> (view.requestFocus()) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); }}</code></pre></pre> <blockquote> <blockquote> <p><strong>note:</strong> Once the input method is ejected, it is best not to use code to hide it. The system hides it after the user terminates the task in the text box or the user hides it through the system (for example, using the back button).</p> </blockquote> </blockquote>Indicate how your UI should respond to the popup of the input method<p><p>When the input method appears on the screen, it reduces the amount of space the app uses on the Screen. Then the system will decide how to adjust some areas of the ui, but it may not be the most Accurate. To ensure that the app has the best user experience, you should indicate how the system adjusts the UI.</p></p><p><p>To declare the preferred mode of activity, you should add the Android:windowsoftinputmode attribute under the activity in the Project's manifest file and use one of the values that contains "adjust".</p></p><p><p>For example, "adjustresize" should be used to ensure that the system can adjust the UI to the available zones:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "><span class="hljs-tag"><<span class="hljs-title">application</span> <span class="hljs-attribute">...</span> ></span> <span class="hljs-tag"><<span class="hljs-title">activity</span> <span class="hljs-attribute">android:windowSoftInputMode</span>=<span class="hljs-value">"adjustResize"</span> <span class="hljs-attribute">...</span> ></span> ... <span class="hljs-tag"></<span class="hljs-title">activity</span>></span> ...<span class="hljs-tag"></<span class="hljs-title">application</span>></span></code></pre></pre><p><p>In addition to the above methods, you can also use a combination of ways to declare UI adjustment rules and input method visibility rules:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class="language-xml hljs "> <span class="hljs-tag"><<span class="hljs-title">activity</span> <span class="hljs-attribute">android:windowSoftInputMode</span>=<span class="hljs-value">"stateVisible|adjustResize"</span> <span class="hljs-attribute">...</span> ></span> ... <span class="hljs-tag"></<span class="hljs-title">activity</span>></span></code></pre></pre><p><p>It is important to specify "adjustresize": if the UI contains keystrokes that users may need to quickly access or text boxes that need to be manipulated. For example, If you use a relative layout to place a button at the bottom of the screen, use "adjustresize" to adjust the layout so that the button appears at the top of the input method so that you can click the button directly after the input is Complete.</p></p> <p><p>Android Official Development Document Training Series Course Chinese version: keyboard input Processing control Input Method display mode</p></p></span>

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.