java - 在以上 Android N 的对话框中,Edittext 无法接收来自键盘的输入

标签 java android android-xml android-dialog

我正在设置一个对话框以从用户处获取 PIN 码。虽然对话框确实通过键盘出现,但 edittext 没有收到来自键盘的输入。同样的对话框在 android 6 上工作。下面是代码

对话框代码

    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View promptsView = layoutInflater.inflate(R.layout.activity_password,
            null);
    Dialog dialog = new Dialog(getApplicationContext()
            , android.R.style.Theme_Black_NoTitleBar_Fullscreen);
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(false);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
    }
    else {
        dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);
    }
    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT);
    dialog.setContentView(promptsView);
    dialog.getWindow().setGravity(Gravity.CENTER);
    dialog.show();

XML 布局代码:

<RelativeLayout
    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"
    android:background="@color/colorAccent">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:text="Enter Your Pin"
        android:textSize="16sp"
        android:gravity="center"
        android:textColor="#fff"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        />
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_centerInParent="true"
        android:hint="Enter Pin"
        android:id="@+id/enter_pin"
        android:inputType="numberPassword"
        android:textAlignment="center"

        />
    <Button
        android:id="@+id/confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enter_pin"
        android:text="Confirm"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="32dp"
        android:layout_marginRight="32dp"
        android:textSize="12dp"
        android:textColor="#fff"
        android:background="@android:color/transparent"
        />
</RelativeLayout>

最佳答案

请在对话框的 Java 文件中尝试以下代码:

                Dialog dialog = new Dialog(MainActivity.this);
                dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.activity_password);
                dialog.setCanceledOnTouchOutside(true);
                dialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.CENTER);
                dialog.show();

此代码适用于上面的 android N 以及下面的 android N。

我希望它对你有用。

关于java - 在以上 Android N 的对话框中,Edittext 无法接收来自键盘的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56168697/

相关文章:

android - 什么是 XML 属性 xmlns :app?

java - 如何让 Spring Data Neo4j 和 Spring Data JPA 协同工作?

java - 如何使用 X-Unsent : 1 using Message. writeTo 将电子邮件保存为 .eml

android - 如何减少android中的图像像素?

Android-Studio git over ssh

android - 如何像9gag一样在android中创建布局

java - 我想在vscode最近的扩展上使用jdk8,如何在setting.json上设置

java - 在 android 中发出自定义列表,每行都有循环

Android - 如何将 View 置于所有其他 Activity 之上,包括其他应用程序(如 Cubie 所做的那样)

java - 错误 :no resource identifier found for attribute"showAsAction"in package android