利用Activity實現一個簡單的可輸入對話框

一、需求分析

在應用中這樣那樣的評論老是少不了的,有的應用是在底部直接加一個EditText和一個Button,讓用戶輸入文字或者表情以後點擊按鈕提交;而有的雖然也放置了EditText,但僅僅是一個「擺設」,並不具有輸入功能,用戶點擊它後會彈出一個跳轉到一個能夠真正編輯的頁面或者彈出一個能夠輸入內容的對話框。好比下面這種效果:java

效果圖

這裏的效果能夠細分爲四點:android

  1. 點擊底部的按鈕以後會彈出對話框,對話框在佈局的底部;
  2. 對話框中有輸入框EditText,能夠輸入內容;
  3. 對話框彈出後EditText會自動獲取焦點,彈出軟鍵盤;
  4. 軟鍵盤會把對話框頂上去,便於用戶編輯。

一開始我想到的是PopupWindow,可是因爲裏面有EditText,與軟鍵盤交互起來非常頭疼,因而改用了Activity。這樣一來咱們就能夠像用Activity同樣使用這個對話框,方便多了。不過畢竟跟咱們平時使用的Activity仍是有所不一樣的,特別是要設置好它的樣式,不然也是一堆的坑啊。git

二、對話框Activity的佈局與樣式

下面就來着手實現咱們想要的對話框了。新建一個工程,MainActivity只是一個配角,底部放一個按鈕就搞定。咱們的主角是DialogActivity,它的佈局很簡單,就跟平時的Activity同樣:app

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <EditText
            android:id="@+id/et_comment"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="15dp"
            android:background="#f0f0f0"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="left|top"
            android:hint="我來說一說~"
            android:paddingBottom="5dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:paddingTop="5dp"
            android:textSize="14dp" />

        <Button
            android:textColor="@android:color/white"
            android:background="@android:color/holo_blue_light"
            android:id="@+id/btn_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:padding="5dp"
            android:text="發表評論"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

重點是它的樣式,看下面的代碼:ide

<!--可輸入對話框的樣式 -->
    <style name="EditDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">
        //設置背景
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowSoftInputMode">adjustResize|stateHidden</item>
        //Dialog的windowFrame框爲無
        <item name="android:windowFrame">@null</item>
        //是否顯示標題,true則去掉默認的標題欄
        <item name="android:windowNoTitle">true</item>
        //是否浮如今activity之上,false的話會被軟鍵盤覆蓋
        <item name="android:windowIsFloating">true</item>
        //是否半透明,爲false時背景爲黑色不透明
        <item name="android:windowIsTranslucent">true</item>
        //是否有覆蓋
        <item name="android:windowContentOverlay">@null</item>
        //Activity的動畫效果
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        //背景是否模糊顯示,爲false時效果爲全透明
        <item name="android:backgroundDimEnabled">true</item>
        //點擊空白處時是否銷燬Activity
        <item name="android:windowCloseOnTouchOutside">true</item>
    </style>

要設置的屬性不少,我都作了註釋了,你們明白每一個屬性的做用就行,這裏就細說了。別忘了,到清單文件中給DialogActivity用上這個主題:佈局

<activity android:name=".DialogActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="portrait"
            android:theme="@style/EditDialogStyle"/>

運行一下,相信你們能夠看到效果了。學習

三、自動彈出軟鍵盤效果

對話框的界面咱們已經作好了,可是爲了用戶體驗更好,咱們要在對話框出現的時候自動彈出軟鍵盤。下面介紹兩種方法:動畫

3.一、使用InputMethodManager類顯示軟鍵盤

咱們平時要讓某個EditText得到焦點自動彈出軟鍵盤能夠這樣寫:spa

InputMethodManager inputManager =(InputMethodManager)etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
   inputManager.showSoftInput(etComment, 0);

可是這裏面有一點要注意:咱們想要讓EditText得到焦點,那必須等界面繪製完畢才行。因此這樣設置了延遲300ms執行彈出軟鍵盤的代碼,給界面留出繪製的時間:.net

new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                InputMethodManager inputManager =
                        (InputMethodManager) etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etComment, 0);
                return false;
            }
        }).sendEmptyMessageDelayed(0, 300);

加上上面的代碼,你就能夠軟鍵盤本身彈出來了。

3.一、設置windowSoftInputMode屬性

你若是細心的話必定發現前面給DialogActivity設置樣式的代碼中有一個windowSoftInputMode屬性沒有添加註釋,請原諒我賣了個關子。這個屬性是設置窗口和軟鍵盤的交互模式的。它的屬性有不少,能夠參考我後面給出的參考文章。這裏咱們用到了adjustResize,它的做用就是調整界面佈局給軟鍵盤留出足夠的空間。那麼stateHidden呢?其實軟鍵盤沒有自動彈出就是它搞的鬼,它表示通常狀況下軟鍵盤都是隱藏的。咱們改爲另一個屬性:stateVisible,它表示軟鍵盤一般是可見的。

再來運行一下,軟鍵盤就如期而至了。

四、後記

咱們在需求分析中提到的效果已經實現完畢。後來我還想過給對話框增長自定義的動畫效果,可是退出時的動畫始終沒有設置成功,因此若是有讀者實現了,歡迎交流學習。源碼我保存到了碼雲,須要的話能夠參考:

可輸入對話框源碼

五、參考文章

android:windowSoftInputMode屬性詳解

完全搞定Android開發中軟鍵盤的常見問題

相關文章
相關標籤/搜索