如何将Dialog转换为DialogFragment?

时间:2018-06-13 15:52:15

标签: java

来自VB世界我今天了解到对话框在Java中并不是模态的。我的任务是使用Android Studio for Android Motorola设备将我们在VB.net中为基于Windows的Motorola设备编写的程序转换为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的调用将在循环期间完成

由于

1 个答案:

答案 0 :(得分: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();
    }
}

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

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

小建议 - 不要在您的小部件dp中使用textSize,请使用sp。您可以阅读解释EXAMPLE