Android學習筆記(二六):IME

來源:互聯網
上載者:User

對Android裝置,可能是無物理鍵盤,可能帶鍵盤,也可能帶數字鍵台,這些都是可以處理的。

inputType:IME可自動適配所設的輸入類型

如果沒有物理鍵盤,當使用者進入EditText的時候,將調起IME(Imput Method Editor)。一般情況下,IME都是智能地彈出,並不需要我們作任何的處理,但是在某些特定的情況下,例如一個多行EditText中,IME會覆蓋部分的部分的內容,這時候,我們就需要考慮了。此外還有密碼輸入,限制類型輸入(數字,電話好嗎,日期,時間等),可以通過android:inputType來進行設定,inputType裡面可以設定多個屬性,實行“|”來分割,主義屬性和"|"之間不要有空格。下面是一個例子:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout ......  android:stretchColumns="1">
      <TableRow>
          <TextView android:text="No Special rules:" />
          <EditText /> <!--inputType預設為text方式-->
      </TableRow>
      <TableRow>
          <TextView android:text="Email address:" />
          <EditText android:inputType="text|textEmailAddress" />
      </TableRow>
      <TableRow>
          <TextView android:text="Signed decimal number:" />
          <EditText android:inputType="number|numberSigned|numberDecimal" />
      </TableRow>
      <TableRow>
          <TextView android:text="Date:" />
          <EditText android:inputType="date" />
      </TableRow>
    <TableRow>
          <TextView android:text="Multi-line text:" />
          <EditText android:inputType="text|textMultiLine|textAutoCorrect"
android:minLines="3" android:gravity="top"/>
      </TableRow>
</TableLayout>

在《Begining Book》中給出的IME和我的模擬器的不一樣,模擬器採用的是T9數字鍵台方式,由於設定了不同的顯示要求,因此鍵盤的出現也會有所不同。如下兩圖所示:

IME可通過確認鍵進行事件觸發

最右下角的button成為accessory button,也就是確認鍵,我們可以通過android:imeOptions來設定相關的處理,例如:

          <TextView android:text="Signed decimal number:" />
          <EditText android:inputType="number|numberSigned|numberDecimal"

            android:imeOptions="actionDone"/>

預設地,按下accessroy button,將會focus到下一個editText,但是我們可以定製這個行文。例如上面的例子,當我們按確認鍵時,IME鍵盤將會消失,表示IME已經處理完了。我們看另外一個例子:

          <EditText android:id="@+id/c10_send"
            android:inputType="text|textEmailAddress"
            android:imeOptions="actionSend"/>

這是一個Email地址格式,我們希望使用者按了IME的確認鍵之後可以進行觸發,在程式中如下處理,我們可以監測到IME_ACTION_SEND的actionId。:

        TextView edit = (TextView)findViewById(R.id.c10_send);
        edit.setOnEditorActionListener(new TextView.OnEditorActionListener()
{
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_SEND)
                    Log.d("WEI","Receive ime : IME_ACTION_SEND ");
                return false;
            }
        });

IME的適配

在上面的例子中,如果我們點擊第一個editText,IME會overlay最下面的widget,如果我們點擊最下面的editText,整個layout就會向上scroll,這種成為pan適配方式。Android可以resize我們的acitivity,使其變小以便IME可以在activity的下面。這種是resize適配。在landscape模式(橫屏)模式下,Android有可能會將IME全屏,覆蓋整個acivity,這允許更大的軟鍵盤方便使用者輸入。

一般的,Android會根據layout的情況選擇pan方式或者resize方式,如果我們需要指定,可以在AndroidManifest.xml檔案中的acivity裡面設定android:windowSoftInputMode屬性,下面是一個例子:

        <activity android:name=".Chapter10.Chapter10Test2" android:label="@string/chapter_10_test2"

          android:windowSoftInputMode="adjustResize"/>

我們將我們上面的例子最後一個EditText中android:minLines="5",使他具有更大空間,下面的兩個圖,左圖採用預設模式,右圖,強制指定採用 android:windowSoftInputMode="adjustResize"模式。

相關連結:我的Andriod開發相關文章

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.