Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂 Dialog 中的 EditText 無法彈出軟鍵盤:原因與解法

編輯:關於 Android 開發

最近獨立開發的專案《全醫會》已在內測階段,很快會上架各大應用市場。之前幾個專案因為種種原因無緣上架,始終有些遺憾,所以這次心情格外好。

今天在做一個新專案,是專為律師與客戶開發的 App,其中有一個自訂對話框的需求。這個知識點其實很簡單,就是下圖的效果:

可是寫完之後才發現:自訂對話框裡嵌套的 EditText 根本無法取得焦點,也彈不出軟鍵盤。有點悶——以前做的自訂對話框裡沒有 EditText,一切正常,一直沒發現這個隱藏的坑。

下面這個是我之前寫的自訂對話框:

一、正確的版面配置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/round_white_corner"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">

            <EditText
                android:id="@+id/et_obj"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="30dp"
                android:background="@drawable/round_textview_corner"
                android:gravity="center_vertical"
                android:hint="輸入標的"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="14sp" />

            <EditText
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="12dp"
                android:background="@drawable/round_textview_corner"
                android:gravity="center_vertical"
                android:hint="聯系人姓名"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="14sp" />

            <EditText
                android:id="@+id/et_phone"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_marginTop="12dp"
                android:background="@drawable/round_textview_corner"
                android:gravity="center_vertical"
                android:hint="聯系人電話"
                android:inputType="phone"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="14sp" />

            <TextView
                android:id="@+id/tv_go"
                android:layout_width="110dp"
                android:layout_height="35dp"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="25dp"
                android:background="@drawable/round_blue_corner"
                android:gravity="center"
                android:text="確認委托"
                android:textColor="@color/white"
                android:textSize="16sp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/iv_close"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:layout_marginLeft="-50dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/law_dialog_close" />
    </LinearLayout>
</LinearLayout>

二、自訂 Dialog 的程式碼

// 彈出自定義 dialog
LayoutInflater inflater = LayoutInflater.from(OnlineActivity.this);
View view = inflater.inflate(R.layout.hl_law_dialog, null);

// 建立對話框:注意這裡用的是 Dialog,不是 AlertDialog
final Dialog dialog = new Dialog(OnlineActivity.this);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

// 設定寬度為螢幕寬度
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = display.getWidth();
dialog.getWindow().setAttributes(lp);

dialog.getWindow().setContentView(view);

final EditText et_obj   = (EditText) view.findViewById(R.id.et_obj);    // 標的
final EditText et_name  = (EditText) view.findViewById(R.id.et_name);   // 姓名
final EditText et_phone = (EditText) view.findViewById(R.id.et_phone);  // 電話
TextView tv_go          = (TextView)  view.findViewById(R.id.tv_go);    // 確認委托
final ImageView iv_close= (ImageView) view.findViewById(R.id.iv_close); // 關閉

三、我錯在哪裡

之前的寫法是:

// 對話框
final Dialog dialog = new AlertDialog.Builder(OnlineActivity.this).create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.show();

差別只有一個:把 AlertDialog 換成 Dialog,問題就解決了。

為什麼 AlertDialog 不行

AlertDialog 不是一個通用的空容器,它是為「標題 + 訊息 + 按鈕列表」這種結構設計的元件。當你用 setContentView() 塞入自訂版面時,AlertDialog 內部的裝飾層與焦點處理機制會與你的內容互相干擾,結果就是內部的 EditText 拿不到焦點,軟鍵盤自然不會彈出。

Dialog 則是一個近乎空白的視窗容器,焦點行為不會被額外介入,自訂版面上的輸入元件因此能正常工作。

這也解釋了為什麼以前沒遇到——那些對話框裡沒有需要輸入文字的元件,焦點問題自然不會浮現。

四、兩段程式碼各自的作用

理解這兩段,能避免以後又抄錯。

如果不加 requestWindowFeature 會怎樣

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

就會出現一個醜醜的標題欄。這行的作用就是去掉系統預設的標題列。

如果不加寬度設定會怎樣

WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = display.getWidth();
dialog.getWindow().setAttributes(lp);

對話框會被壓得很窄,因為 Dialog 預設是依內容包裹(wrap content)。這段程式碼的作用是把寬度撐到整個螢幕寬。

五、還有一個順序陷阱

仔注意:requestWindowFeature() 必須在 setContentView() 之前呼叫。若順序顛倒,系統已經完成視窗內容的佈局,此時再要求移除標題列會直接拋出 AndroidRuntimeException,訊息是「requestFeature() must be called before adding content」。

示範程式碼中的順序是正確的:setBackgroundDrawable → requestWindowFeature → show → 設定寬度 → setContentView。實務上更常見的寫法是把 requestWindowFeature 放在 setContentView 前、show() 前,順序清楚就好。

六、如果一定要用 AlertDialog

在某些既有專案中,AlertDialog.Builder 已被大量使用、難以替換。這時可以嘗試:

  • 透過 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) 明確要求視窗為鍵盤調整大小。
  • 在 show() 之後呼叫 et_obj.requestFocus() 主動搶焦點。
  • 避免在自訂版面上設定 <requestFocus /> 之外的焦點搶佔屬性。

但這些都是繞路。若版面本身就是自訂的,直接改用 Dialog 是更乾淨、也更可預期做法。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved