在AlertDialog中使用自定義的View,如果View中有EditText,在上面點擊,默認是跳不出軟鍵盤的,不是焦點的問題。
解決方法,有兩種,一是把AlertDialog換成Dialog,但這么一來,對話框的最外層會多出一個框,頂部還會空幾十個DP,當然可以用setBackgroundDrawable(new ColorDrawable(0))把背景設為透明,隱藏掉邊框,但是上面空着的幾十個DP還在,對話框就不是在屏幕居中了。
代碼:
Dialog ad = new Dialog(context); ad.show(); Window window = ad.getWindow(); window.setBackgroundDrawable(new ColorDrawable(0)); window.setContentView(R.layout.cancel_sos_dialog);
最好的辦法是第二種:
AlertDialog ad = new AlertDialog.Builder(context).create(); ad.setView(ManagerDialogLayout_.build(context,ad)); ad.show(); Window window = ad.getWindow(); window.setContentView(ManagerDialogLayout_.build(context,ad));
在調用show方法前先調用setView(layout),show后再調用window.setContentView(layout),兩個Layout布局應該是相同的。
至於原因,暫時不明,但是確實解決了問題,在EditText上點擊,可以調出軟鍵盤,輸入法了。
2013年1月6日:第一種方法的BUG,解決方法:
使用自定義的Style:
<style name="CustomDialogStyle" parent="@Android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:backgroundDimAmount">0.6</item> </style> Dialog ad = new Dialog(context,R.style.CustomDialogStyle);