gpt4 book ai didi

java - 关于自定义对话框的问题

转载 作者:行者123 更新时间:2023-12-01 11:38:16 27 4
gpt4 key购买 nike

我在对话框方面遇到了麻烦,所以我已经多次重新阅读了 android 文档,但我仍然不确定以下事情,如果有人能回答我的问题,我将非常感激......在我提出问题之前,我会展示我的代码......

CustomDialog(直接从 android 开发站点复制)

public class FireMissilesDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();

// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_createlocation, null))
.setTitle(R.string.dialog_createlocationtitle)
// Add action buttons
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
FireMissilesDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}`

这是对话框的布局(dialog_createlocation.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/EditTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/name"
android:maxLines="1"/>
<EditText
android:id="@+id/EditTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/address"
android:maxLines="2"/>

问题:/n

  1. 在我的主要 Activity 中,我想从对话框中的两个 EditText 获取文本。虽然我看到了一些关于此的问题,但我非常不知所措,似乎无法理解答案。/n

2.我是否有必要在自己的类中创建此对话框?-我可以在我的主要 Activity 中创建它吗(-而不创建内部类)?/n

3.我很困惑为什么要创建自定义对话框,它必须扩展一个 fragment - 为什么不仅仅是一个 Activity ?/n

4.我在我的主要 Activity (不是 fragment )中创建了上述对话框的实例,但在执行此操作时遇到了一些问题:

public void showNoticeDialog() {
// Create an instance of the dialog fragment and show it
DialogFragment dialog = new FireMissilesDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");

}

谢谢!

最佳答案

  1. In my main activity, I want to get the text from the two EditText in the dialog. Although Ive seen some SO questions about this but im so overwelmed and cant seem to understand the answers.
EditText editTextName = dialog.getDialog().findViewById(R.id.EditTextName);
String name = editTextName.getText().toString();
  1. Is it necessary for me to create this dialog in its own class?-can i just create it in my main activity(- without creating an inner class)?

是的,可以。 AlertDialog 只是为您提供对话框已有的结构。但要制作自己的,只需使用 Dialog 类。

3.Im confused with why to create a custom dialog, it has to extend a fragment-why not just an activity?

Dialog 不必只使用 Fragment。按照第二个答案。

4.I create an instance of the above dialog in my main activity (which is not a fragment) and i got some issues doing this:

为此发布堆栈跟踪或错误日志。

关于java - 关于自定义对话框的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29783217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com