簡體   English   中英

有關自定義對話框的問題

[英]Questions regarding custom dialog

我在對話方面遇到麻煩,因此我已經多次閱讀了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. 在我的主要活動中,我想從對話框中的兩個EditText中獲取文本。 盡管我已經看到了一些有關此的問題,但是我太過熱情了,似乎無法理解答案。

2,我是否有必要在自己的類中創建此對話框?-我可以在我的主要活動中創建它嗎(-而無需創建內部類)?/ n

3.我對為什么要創建自定義對話框感到困惑,它必須擴展一個片段,為什么不只是一個活動?/ n

4.我在我的主要活動中創建了上述對話框的一個實例(不是片段),並且在執行此操作時遇到了一些問題:

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

}

謝謝!

  1. 在我的主要活動中,我想從對話框中的兩個EditText中獲取文本。 盡管我已經看到了一些有關此的問題,但是我太過熱情了,似乎無法理解答案。
EditText editTextName = dialog.getDialog().findViewById(R.id.EditTextName);
String name = editTextName.getText().toString();
  1. 我是否需要在自己的類中創建此對話框?-我可以在我的主要活動中創建它嗎(-而無需創建內部類)?

是的 ,可以。 AlertDialog只是給您已經存在的對話框結構。 但是要制作自己的Dialog請使用Dialog Class。

3.我對為什么要創建自定義對話框感到困惑,它必須擴展一個片段-為什么不只是一個活動?

不必僅對對話框使用片段。 按照每秒的答案。

4.我在我的主要活動中創建了上述對話框的一個實例(不是片段),並且在執行此操作時遇到了一些問題:

為此,發布stacktrace或錯誤日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM