1、需求分析
眾所周知,在應用中這樣那樣的評論總是少不了的,有的應用是在底部直接加一個EditText和一個Button,讓用戶輸入文字或者表情之后點擊按鈕提交;而有的雖然也放置了EditText,但僅僅是一個“擺設”,并不具備輸入功能,用戶點擊它后會彈出一個跳轉到一個可以真正編輯的頁面或者彈出一個可以輸入內容的android/221269.html">對話框。
比如下面這種效果:

這里的效果可以細分為四點:
一開始我想到的是PopupWindow,但是由于里面有EditText,與軟鍵盤交互起來很是頭疼,于是改用了Activity。這樣一來我們就可以像用Activity一樣使用這個對話框,方便多了。不過畢竟跟我們平時使用的Activity還是有所不同的,特別是要設置好它的樣式,否則也是一堆的坑啊。
2、對話框Activity的布局與樣式
下面就來著手實現我們想要的對話框了。新建一個工程,MainActivity只是一個配角,底部放一個按鈕就搞定。我們的主角是DialogActivity,它的布局很簡單,就跟平時的Activity一樣:
<?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>
重點是它的樣式,看下面的代碼:
<!--可輸入對話框的樣式 --> <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、自動彈出軟鍵盤效果
對話框的界面我們已經做好了,但是為了用戶體驗更好,我們要在對話框出現的時候自動彈出軟鍵盤。下面介紹兩種方法:
3.1、使用InputMethodManager類顯示軟鍵盤
我們平時要讓某個EditText獲得焦點自動彈出軟鍵盤可以這樣寫:
InputMethodManager inputManager =(InputMethodManager)etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(etComment, 0);
但是這里面有一點要注意:我們想要讓EditText獲得焦點,那必須等界面繪制完畢才行。所以這樣設置了延遲300ms執行彈出軟鍵盤的代碼,給界面留出繪制的時間:
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.1、設置windowSoftInputMode屬性
你如果細心的話一定發現前面給DialogActivity設置樣式的代碼中有一個windowSoftInputMode屬性沒有添加注釋,請原諒我賣了個關子。這個屬性是設置窗口和軟鍵盤的交互模式的。它的屬性有很多,可以參考我后面給出的參考文章。這里我們用到了adjustResize,它的作用就是調整界面布局給軟鍵盤留出足夠的空間。那么stateHidden呢?其實軟鍵盤沒有自動彈出就是它搞的鬼,它表示一般情況下軟鍵盤都是隱藏的。我們改成另外一個屬性:stateVisible,它表示軟鍵盤通常是可見的。
再來運行一下,軟鍵盤就如期而至了。
4、后記
我們在需求分析中提到的效果已經實現完畢。后來我還想過給對話框增加自定義的動畫效果,但是退出時的動畫始終沒有設置成功,所以如果有讀者實現了,歡迎交流學習。
源碼我保存到了碼云,需要的話可以參考:可輸入對話框源碼
大家也可以通過本地下載:點擊這里
好了,以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。
新聞熱點
疑難解答