簡體   English   中英

數字鍵盤不適用於 TextInputLayout(在對話框中)

[英]Numeric keyboard not working with TextInputLayout (In Dialog)

我在 TextInputLayout 中遇到了奇怪的問題。

我的問題:

如果我給輸入類型“文本”它工作正常。

但是如果我給輸入類型“數字”它只是顯示鍵盤。 但無法在edittext中輸入數字。

我的xml代碼:

       <android.support.design.widget.TextInputLayout
                android:id="@+id/tl_age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/tl_dob"
                app:layout_marginLeftPercent="2%">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/patient_age"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edittext_bg"
                    android:hint="Age"
                    android:inputType="number"
                    android:maxLength="3"
                    android:maxLines="1"
                    android:minHeight="@dimen/min_ed_height"
                    />

            </android.support.design.widget.TextInputLayout>

我嘗試了什么:

  1. 從 Java 代碼設置 TextInputType --> mEtAge.setRawInputType(InputType.TYPE_CLASS_NUMBER);

  2. 以前我現在使用 Edittext TextInputEditText

  3. 設置 TextChange 偵聽器(用於測試)-> 但未觸發 textChange 偵聽器。

  4. 在 Google 和 SO 中搜索。 沒有找到類似的問題。

Java代碼:

我在我的 Dialog(android.app.Dialog) 中使用此代碼。

public void showBookAppointmentDialog(final Activity activity){
      mDialog = new Dialog(activity,android.R.style.Theme_Light_NoTitleBar_Fullscreen);
      mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      mDialog.setContentView(R.layout.sv_existinguser_appointment);
      mDialog.setCancelable(false);
      mEtAge = (TextInputEditText) mDialog.findViewById(R.id.patient_age);
      .....
      }

感謝您的回答。

正如 OP 所建議的,這就是為我解決問題的方法。

我已經為我返回 true 的對話框設置了一個 OnKeyListener。 由於某種原因,數字鍵觸發了這個 OnKeyListener ,因此沒有輸入。

因此,當我的條件不滿足時,我將偵聽器更改為返回 false,這為我解決了這個問題。

dialog?.setOnKeyListener { _, keyCode, event ->
    if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_UP) {
        if (!getNavController().popBackStack()) {
            dismissAllowingStateLoss()
            return@setOnKeyListener true
        }
    }
    false
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM