java - 如何将 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 的调用也将在循环期间完成
谢谢
解决方案
首先扩展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
. 你可以在这里阅读解释。
推荐阅读
- r - organizing the dataset and sorting the edited data
- python - 有没有办法解决这个方法调用错误?
- android - 升级到 androidX 后 PlayStore 崩溃 ClassNotFoundException
- visual-studio-code - 如何在不丢失格式的情况下将代码从 vscode 复制到 OneNote?
- android - 基于传递给片段的参数,具有不同 ViewModel 类型的相同片段类型的多个实例
- javascript - 从客户端调整图像大小以进行多个 HTML 输入
- autohotkey - 如何使“BlockInput On/Off”仅阻止键盘输入而不是鼠标
- python - 尽管之前已经定义了名称错误?
- python - Tkinter:如果我需要从 asksaveasfile 打印路径的问题
- pandas - 熊猫将数组列转换为具有条件的多列