Android的输入法

jopen 12年前
     <p>对Android设备,可能是无物理键盘,可能带键盘,也可能带数字键盘,这些都是可以处理的。</p>    <p><strong>inputType:IME可自动适配所设的输入类型</strong></p>    <p>如果没有物理键盘,当用户进入EditText的时候,将调起IME(Imput Method Editor)。一般情况下,IME都是智能地弹出,并不需要我们作任何的处理,但是在某些特定的情况下,例如一个多行EditText中,IME会覆盖部分的部分的内容,这时候,我们就需要考虑了。此外还有密码输入,限制类型输入(数字,电话好吗,日期,时间等),可以通过 android:inputType来进行设定,inputType里面可以设置多个属性,实行“|”来分割,主义属性和"|"之间不要有空格。下面是一个例子<span style="color:#888888;">:</span></p>    <blockquote style="border-bottom:#ffbcbc thin dashed;border-left:#ffbcbc thin dashed;padding-bottom:5pt;line-height:140%;background-color:#ffffea;margin-top:11px;text-indent:0em;padding-left:5pt;padding-right:5pt;font-family:微软雅黑;margin-bottom:11px;margin-left:10pt;font-size:9pt;border-top:#ffbcbc thin dashed;border-right:#ffbcbc thin dashed;padding-top:5pt;">     <p><span style="color:#888888;"><?xml version="1.0" encoding="utf-8"?><br /> <TableLayout ......  android:stretchColumns="1"></span><br /> <span style="color:#888888;">      <TableRow></span><br />           <TextView android:text="No Special rules:" /><br />           <EditText /> <span style="color:#3366ff;"><!--inputType缺省为text方式--></span><br /> <span style="color:#888888;">      </TableRow></span><br /> <span style="color:#808080;">      <TableRow></span><br /> <span style="color:#808080;">          <TextView android:text="Email address:" /></span><br />           <EditText <strong>android:inputType="text|textEmailAddress"</strong> /><br /> <span style="color:#808080;">      </TableRow><br />       <TableRow></span><br />           <TextView android:text="Signed decimal number:" /><br />           <EditText <strong>android:inputType="number|numberSigned|numberDecimal"</strong> /><br /> <span style="color:#808080;">      </TableRow><br />       <TableRow></span><br />           <TextView android:text="Date:" /><br />           <EditText<strong> android:inputType="date"</strong> /><br /> <span style="color:#808080;">      </TableRow><br />     <TableRow></span><br />           <TextView android:text="Multi-line text:" /><br />           <EditText<strong> android:inputType="text|textMultiLine|textAutoCorrect"</strong>android:minLines="3" android:gravity="top"/><br /> <span style="color:#808080;">      </TableRow></span><br /> <span style="color:#888888;"></TableLayout></span></p>    </blockquote>    <p>在《Begining Book》中给出的IME和我的模拟器的不一样,模拟器采用的是T9数字键盘方式,由于设置了不同的显示要求,因此键盘的出现也会有所不同。如下两图所示:</p>    <p><img title="Android的输入法" border="0" alt="Android的输入法" src="https://simg.open-open.com/show/348bde5d37f7a3f22fc5a53d89b05f09.png" width="381" height="303" /></p>    <p><strong>IME可通过确认键进行事件触发</strong></p>    <p>最右下角的button成为accessory button,也就是确认键,我们可以通过android:imeOptions来设置相关的处理,例如:</p>    <blockquote style="border-bottom:#ffbcbc thin dashed;border-left:#ffbcbc thin dashed;padding-bottom:5pt;line-height:140%;background-color:#ffffea;margin-top:11px;text-indent:0em;padding-left:5pt;padding-right:5pt;font-family:微软雅黑;margin-bottom:11px;margin-left:10pt;font-size:9pt;border-top:#ffbcbc thin dashed;border-right:#ffbcbc thin dashed;padding-top:5pt;">     <p><span style="color:#888888;">          <TextView android:text="Signed decimal number:" /><br />           <EditText android:inputType="number|numberSigned|numberDecimal" </span><br />             android:imeOptions="<strong>actionDone</strong>"/></p>    </blockquote>    <p>缺省地,按下accessroy button,将会focus到下一个editText,但是我们可以定制这个行文。例如上面的例子,当我们按确认键时,IME键盘将会消失,表示IME已经处理完了。我们看另外一个例子:</p>    <blockquote style="border-bottom:#ffbcbc thin dashed;border-left:#ffbcbc thin dashed;padding-bottom:5pt;line-height:140%;background-color:#ffffea;margin-top:11px;text-indent:0em;padding-left:5pt;padding-right:5pt;font-family:微软雅黑;margin-bottom:11px;margin-left:10pt;font-size:9pt;border-top:#ffbcbc thin dashed;border-right:#ffbcbc thin dashed;padding-top:5pt;">     <p><span style="color:#888888;">          <EditText android:id="@+id/c10_send" <br />             android:inputType="text|textEmailAddress" </span><br />             android:imeOptions="actionSend"/></p>    </blockquote>    <p>这是一个Email地址格式,我们希望用户按了IME的确认键之后可以进行触发,在程序中如下处理,我们可以监测到IME_ACTION_SEND的actionId。:</p>    <blockquote style="border-bottom:#ffbcbc thin dashed;border-left:#ffbcbc thin dashed;padding-bottom:5pt;line-height:140%;background-color:#ffffea;margin-top:11px;text-indent:0em;padding-left:5pt;padding-right:5pt;font-family:微软雅黑;margin-bottom:11px;margin-left:10pt;font-size:9pt;border-top:#ffbcbc thin dashed;border-right:#ffbcbc thin dashed;padding-top:5pt;">     <p><span style="color:#888888;">        TextView edit = (TextView)findViewById(R.id.c10_send);</span><br />         edit.<strong>setOnEditorActionListener</strong>(<strong>new TextView.OnEditorActionListener()</strong>{<br />             public boolean <strong>onEditorAction</strong>(TextView v, int actionId, KeyEvent event) {<br />                 if(actionId == <strong>EditorInfo.IME_ACTION_SEND</strong>)<br /> <span style="color:#888888;">                    Log.d("WEI","Receive ime : IME_ACTION_SEND ");<br /> </span><span style="color:#888888;">                return false;<br />             }<br />         });</span></p>    </blockquote>    <p><strong>IME的适配</strong></p>    <p>在上面的例子中,如果我们点击第一个editText,IME会overlay最下面的widget,如果我们点击最下面的editText,整个 layout就会向上scroll,这种成为pan适配方式。Android可以resize我们的acitivity,使其变小以便IME可以在 activity的下面。这种是resize适配。在landscape模式(横屏)模式下,Android有可能会将IME全屏,覆盖整个 acivity,这允许更大的软键盘方便用户输入。</p>    <p>一般的,Android会根据layout的情况选择pan方式或者resize方式,如果我们需要指定,可以在AndroidManifest.xml文件中的acivity里面设置android:windowSoftInputMode属性,下面是一个例子:</p>    <blockquote style="border-bottom:#ffbcbc thin dashed;border-left:#ffbcbc thin dashed;padding-bottom:5pt;line-height:140%;background-color:#ffffea;margin-top:11px;text-indent:0em;padding-left:5pt;padding-right:5pt;font-family:微软雅黑;margin-bottom:11px;margin-left:10pt;font-size:9pt;border-top:#ffbcbc thin dashed;border-right:#ffbcbc thin dashed;padding-top:5pt;">     <p>        <activity android:name=".Chapter10.Chapter10Test2" android:label="@string/chapter_10_test2"<br />           android:windowSoftInputMode="adjustResize"/></p>    </blockquote>    <p>我们将我们上面的例子最后一个EditText中android:minLines="5",使他具有更大空间,下面的两个图,左图采用缺省模式,右图,强制指定采用 android:windowSoftInputMode="adjustResize"模式。</p>    <p><img title="Android的输入法" border="0" alt="Android的输入法" src="https://simg.open-open.com/show/348bde5d37f7a3f22fc5a53d89b05f09.png" width="381" height="303" /></p>