在上方android N的对话框中时,edittext无法从键盘接收输入

时间:2019-05-16 12:34:32

标签: java android android-xml android-dialog

我正在建立一个对话框,以从用户那里获取密码。虽然对话框确实与键盘一起出现,但是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>

1 个答案:

答案 0 :(得分:0)

请尝试使用以下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以下。

希望它对您有用。