首页 > 解决方案 > 如何将 Dialog 转换为 DialogFragment?

问题描述

来自 VB 世界,我今天了解到对话框在 Java 中不是模态的。我的任务是使用适用于 Android 摩托罗拉设备的 Android Studio 将我们在 VB.net 中为基于 Windows 的摩托罗拉设备编写的程序转换为 Java。我正在尝试使其类似于 Windows 中的表单。我在一个有几个提示输入的消息框的表单中。我试过做java,但注意到对话框并不总是等待响应。我已经读过使用 dialogfragments 可以完成我想要做的事情。这是我在第一个对话框中使用的 XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:background="@color/colorBackgroundGray"
    >

    <TextView
        android:id="@+id/theText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Holding Text"
        android:textAlignment="center"
        android:textColor="@color/colorBlack"
        android:textSize="30dp" />


    <Button
        android:id="@+id/buttonYes"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/message"
        android:layout_margin="8dp"
        android:onClick="onClickYes"
        android:text="Yes"
        android:textSize="26dp" />

    <Button
        android:id="@+id/buttonNo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/message"
        android:layout_margin="8dp"
        android:onClick="onClickNo"
        android:text="No"
        android:textSize="26dp" />


</RelativeLayout>

这是我调用对话框的方式。

 final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
                                dialog.setContentView(R.layout.dialog_location);
                                dialog.setTitle("New Location Setup");
                                TextView message = (TextView) dialog.findViewById(R.id.theText);
                                message.setText("Is this location a Pick Bin");
                                Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
                                Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
                                dialog.show();

对需要进行哪些更改才能将对话框转换为 dialogFragment 有疑问。我很欣赏任何人都可以摆脱的清晰。dialogFragments 的调用也将在循环期间完成

谢谢

标签: java

解决方案


首先扩展DialogFragment并使用AlertDialog.Builder在方法里面创建对话框onCreateDialog

public class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(inflater.inflate(R.layout.dialog_location, null))
               .setMessage("Is this location a Pick Bin")
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {                       
                       // Handle "yes" button click here.
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // Handle "no" button click here.
                   }
               });
        return builder.create();
    }
}

然后你可以DialogFragment通过使用show()方法显示:

DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "MyDialogFragment")

小建议 - 不要dp在你的小部件textSize中使用,使用sp. 你可以在这里阅读解释。


推荐阅读