【问题标题】:How to transform Dialog into DialogFragment?如何将 Dialog 转换为 DialogFragment?
【发布时间】:2018-06-14 00:56:56
【问题描述】:

来自 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


    【解决方案1】:

    首先扩展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();
        }
    }
    

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

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

    小建议 - 不要在您的小部件中使用 dp textSize,使用 sp。您可以阅读说明here

    【讨论】:

    • 获取无法解析的方法 'setCONntentView(int)'
    • 行 public classmydialog 带有下划线 wit message of This fragment 内部类应该是静态的。
    • 把这个类放到单独的java文件中。
    • 在循环中调用它。循环是否也必须在课堂上。 MyDialogFragment 程序外部正在执行循环而不显示对话框。这是否意味着每次循环迭代下一个 i 时显示对话框?
    • 我看到了。这是我从数据库返回两条记录的情况。我需要检查它们中的每一个是否要设置为特定状态。所以我有 for (i = 0; i
    猜你喜欢
    • 2011-12-20
    • 2022-01-21
    • 1970-01-01
    • 2021-08-13
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多