我应该使用什么来代替 android api 中的自定义对话框 less 20

What should I use instead of custom dialog in android api less 20

我编写了一个必须调用对话框才能获取名称的应用程序。我需要对话框中的自定义视图,所以我使用了 .setContentView()。但是在 android 中 api 少于 20 我在显示它时遇到了问题。 这是调用对话框:

public void setName(View v) {
        nameDialog = new Dialog(this);
        //using our dialog
        nameDialog.setContentView(R.layout.new_name);
        EditText text = (EditText) nameDialog.findViewById(R.id.form2_dialog_name);
        //if we have previous name we show it
        if (haveName) {
            text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
        }
        //request focus and call keyboard for input name
        text.requestFocus();
        text.selectAll();
        nameDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        nameDialog.show();
    }

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_dark"
    tools:context=".CreateNoteActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/form2_dialog_name_of_event"
        android:id="@+id/textView11" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text=""
        android:ems="10"
        android:id="@+id/form2_dialog_name"
        android:maxLines="1" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end">

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/close_button"
            android:id="@+id/button8"
            android:onClick="onCancelDialog"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/okay_button"
            android:id="@+id/okay_form2_dialog_button"
            android:onClick="onFinishDialog" />
    </LinearLayout>

</LinearLayout>

api 高于 20 时的外观: http://i.imgur.com/pJ1oZky.png

20 人以内: http://i.imgur.com/TJGEwXh.png

好的,答案很简单:使用 android.support.v7.app.AlertDialog

您的 setName 必须如下所示:

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.new_name, null);
    builder.setView(dialogView);

    EditText text = (EditText) dialogView.findViewById(R.id.form2_dialog_name);
    //if we have previous name we show it
    if (haveName) {
        text.setText(((Button) findViewById(R.id.form2_name_button)).getText());
    }
    //request focus and call keyboard for input name
    text.requestFocus();
    text.selectAll();

    AlertDialog dialog = builder.create();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    dialog.show();

我想您需要稍微调整一下布局文件,因为按钮离得太远了,但我认为这只是个小问题 ;)