Ui effect (6): edittext and keypad

Source: Internet
Author: User

Edittext requires a lot of attention, so make a summary.

This blog will be updated continuously.

The following content is from: http://www.learningandroid.net/blog.

The following layout definition file provides some examples to illustrate the use of inputtype and imeoptions.

<EditText android:id="@+id/textNormal"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Normal text"android:inputType="text"android:imeOptions="actionNext"/><EditText android:id="@+id/textInteger"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Integer only"android:inputType="number"android:imeOptions="actionNext"/><EditText android:id="@+id/textDecimal"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Decimal only"android:inputType="numberDecimal"android:imeOptions="actionNext"/><EditText android:id="@+id/textPhone"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Phone number"android:inputType="phone"android:imeOptions="actionNext"/><EditText android:id="@+id/textEmail"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Email"android:imeOptions="actionSend"android:inputType="textEmailAddress"/><EditText android:id="@+id/textSite"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Web Site"android:imeOptions="actionDone"android:inputType="textUri"/>


As you can see, the keyboard of the Input Method automatically changes with the inputtype, and English characters are not allowed when inputtype is set to number.

In addition, in the inputtype attribute, textcapcharacters is an uppercase character. textcapword is the first letter of a word.

Note: Android: phonenumber, Android: numeric, these attributes have been deprecated and many input methods are no longer supported. It is better to use inputtype directly.

In addition, it is best to use Google pinyin or android keyboard for such debugging. Otherwise, imeoptions may not be displayed normally. For example, Baidu input method does not support imeoptions until I delete it.

We often encounter a button that needs to be clicked in the input method window. Only the back key can be used to close the input method. We can solve this problem by modifying the input method of the window.

Another option that may be useful is whether the input method window is immediately displayed when the window is opened, or if the user clicks the text box and then pops up. The latter is usually applicable to the logon screen.

File Name: input_method_test.xml

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:fillViewport="true"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><EditText android:id="@+id/textNormal"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Normal text"android:inputType="text"android:imeOptions="actionNext"/><EditText android:id="@+id/textInteger"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Integer only"android:inputType="number"android:imeOptions="actionNext"/><EditText android:id="@+id/textDecimal"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Decimal only"android:inputType="numberDecimal"android:imeOptions="actionNext"/><EditText android:id="@+id/textPhone"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Phone number"android:inputType="phone"android:imeOptions="actionNext"/><EditText android:id="@+id/textEmail"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Email"android:imeOptions="actionSend"android:inputType="textEmailAddress"/><EditText android:id="@+id/textSite"android:layout_width="fill_parent" android:layout_height="wrap_content"android:hint="Web Site"android:imeOptions="actionDone"android:inputType="textUri"/><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="wrap_content"><ToggleButton android:id="@+id/btnResize"android:layout_width="wrap_content" android:layout_height="wrap_content"android:textOff="Pan" android:textOn="Resize"/><ToggleButton android:id="@+id/btnHidden"android:layout_width="wrap_content" android:layout_height="wrap_content"android:textOff="Visible" android:textOn="Hidden"/></LinearLayout></LinearLayout></ScrollView>

The last two tooglebuttons are the two switches we use to control the keyboard. Then paste the Code:
File Name: inputmethodtestactivity. Java

Package mark. zhang; import android. app. activity; import android. content. sharedpreferences; import android. OS. bundle; import android. view. windowmanager; import android. widget. compoundbutton; import android. widget. togglebutton;/*** input box input method control and Input Method window control */public class inputmethodtestactivity extends activity implementscompoundbutton. oncheckedchangelistener {sharedpreferences prefs; Private Static final stri Ng pref_adjust = "adjust"; Private Static final string pref_hidden = "hidden"; Private Static final string pref_package = "input_method_test"; // togglebutton instance and its corresponding parameter togglebutton btnresize; togglebutton btnhidden; private Boolean resize; private Boolean hidden; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. input_method_test); // obtain Save the parameter values, and set the display mode of the input method in this window. prefs = getsharedpreferences (pref_package, mode_private); resize = prefs. getboolean (pref_adjust, false); Hidden = prefs. getboolean (pref_hidden, false); setupinputwindow (); // set two toggle buttonbtnresize = (togglebutton) findviewbyid (R. id. btnresize); btnresize. setchecked (resize); btnresize. setoncheckedchangelistener (this); btnhidden = (togglebutton) findviewbyid (R. id. btnhidden); btnh Idden. setchecked (hidden); btnhidden. setoncheckedchangelistener (this) ;}@ overridepublic void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {// attributes are saved when the toggle button changes at any time, and reset the INPUT attribute resize = btnresize. ischecked (); Hidden = btnhidden. ischecked (); sharedpreferences. editor editor = prefs. edit (); Editor. putboolean (pref_adjust, resize); Editor. putboolean (pref_hidden, hidden); Editor. commit (); s Etupinputwindow ();}/*** set the properties of the input Window Based on the resize and hidden parameters */private void setupinputwindow () {int inputmode = resize? Windowmanager. layoutparams. soft_input_adjust_resize: windowmanager. layoutparams. soft_input_adjust_pan; inputmode = inputmode | (hidden? Windowmanager. layoutparams. soft_input_state_hidden: windowmanager. layoutparams. soft_input_state_visible); getwindow (). setsoftinputmode (inputmode );}}

There are also several parameters, which are described as follows:

  • Visibility states: soft_input_state_unspecified, soft_input_state_unchanged, soft_input_state_hidden, soft_input_state_always_visible, or soft_input_state_visible.
  • Adjustment options: soft_input_adjust_unspecified, soft_input_adjust_resize, or soft_input_adjust_pan.

If both of the two switches are on:

We can see that the resize mode is used, and the size of the entire activity is reduced, giving up the space of the input form, and the activity itself uses scrollview, so the activity can be rolled to the bottom.


Article link:

Http://www.learningandroid.net/blog/foundation/input-method-control-in-edittext/

Http://www.learningandroid.net/blog/foundation/input-method-soft-mode/

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.