Java 如何将对话框转换为DialogFragment?
我今天从VB世界学到了对话在Java中不是模态的。我的任务是将我们用VB.net为基于windows的摩托罗拉设备编写的程序转换为使用Android Studio为Android摩托罗拉设备编写的Java程序。 我正试图使其与windows中的窗体类似。 我所在的表单有几个提示输入的MessageBox。 我尝试使用java,但注意到对话框并不总是等待响应。 我已经读到,使用dialogfragments可以完成我想要做的事情。 这是我在第一个对话框中使用的XMLJava 如何将对话框转换为DialogFragment?,java,Java,我今天从VB世界学到了对话在Java中不是模态的。我的任务是将我们用VB.net为基于windows的摩托罗拉设备编写的程序转换为使用Android Studio为Android摩托罗拉设备编写的Java程序。 我正试图使其与windows中的窗体类似。 我所在的表单有几个提示输入的MessageBox。 我尝试使用java,但注意到对话框并不总是等待响应。 我已经读到,使用dialogfragments可以完成我想要做的事情。 这是我在第一个对话框中使用的XML <?xml versio
<?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>
对将对话框转换为dialogFragment需要做哪些更改有疑问。我欣赏任何人都可以摆脱的清晰。此外,dialogFragments的调用将在循环期间完成
感谢首先扩展
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
,使用sp
。您可以阅读解释。获取无法解析的方法“setCOntentView(int)”行public classmydialog带下划线,该片段的消息内部类应为静态。将该类放入单独的java文件中。在循环期间调用该类。循环是否也必须在课堂上进行。MyDialogFragment程序外部正在执行循环,而不显示对话框。这意味着每次循环迭代下一个iI时都会显示对话框,我看到了。下面是我从数据库返回的两条记录。我需要检查它们是否都要设置为特定状态。所以我有for(I=0;IDialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "MyDialogFragment")