Android自己定義dialog中的EditText無法彈出鍵盤的解決


近期我獨立開發的項目《全醫會》已經在內測其中了。非常快將會上架到各大應用市場。之前開發的幾個項目都由於一些原因沒有上架還是比較遺憾的。所以,近期我心情格外的好。
今天在做一個新項目,專為律師和客戶開發的APP。其中有一個自己定義對話框的需求。這個知識點事實上非常easy,就是下圖這個效果:
這里寫圖片描寫敘述
但是當我悠閑的寫完以后才發現。自己定義對話框里面嵌套的EditText根本無法獲取焦點。無法彈出軟鍵盤,郁悶,曾經開發的軟件里面沒有EditText的時候一切正常。沒有發現這個隱藏的坑。下圖是我之前寫的一個自己定義對話框:
這里寫圖片描寫敘述


以下來解決問題:
1、對話框的布局文件:

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://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" style="@style/DarkTextViewTheme" 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" style="@style/DarkTextViewTheme" 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" style="@style/DarkTextViewTheme" 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" > </TextView> </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>

這里寫圖片描寫敘述

2、自己定義dialog的代碼


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

        // 對話框
        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 = (int) (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就可以解決問題。

試想:
1、假設不加這段代碼是什么效果?

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

這里寫圖片描寫敘述
如圖。出現了丑陋的標題欄。

2、假設不加這段代碼是什么效果?

WindowManager windowManager = getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); // 設置寬度
        dialog.getWindow().setAttributes(lp);

這里寫圖片描寫敘述
如圖。對話框變得這么窄。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM