2

最后,我发现了问题所在...

如果你有这样的功能:

public void test()
{
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
}

该对话框只会在 test() 完成时出现,我不确定这是否是 Android 对话框工作的唯一方式,但我肯定会阅读更多关于这...


原始问题:

我是android世界的新手,有人可以解释一下吗?

dlg.show() 无异常执行,但什么也没发生,我该怎么做才能知道出了什么问题?该项目使用 Android 2.2 的 API。

public class MainActivity extends FragmentActivity
{
    ...
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
}

对话框布局:

<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/username"
    android:inputType="textEmailAddress"
    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="username" />
<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    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:hint="password"/>
</LinearLayout>

对话类:

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class LoginDialog 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.login_dialog, null))
        // Add action buttons
               .setPositiveButton("Sign In", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // sign in the user ...
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        // do something
                   }
               });
        return builder.create();
    }
}
4

6 回答 6

3

尝试使用 newInstance() 创建您的片段

public class LoginDialog extends DialogFragment
{
    static LoginDialog newInstance(String title) {
        LoginDialog f = new LoginDialog();
        Bundle args = new Bundle();
        args.putString("title", title);
        f.setArguments(args);
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        .
        .
        .
    }
}

然后在你的 MainActivity

public class MainActivity extends Activity // notice that in my case I extend Activity instead of FragmentActivity
{
    @Override
    public void onCreate(Bundle savedInsatnceState) {
        super.onCreate(savedInsatnceState);
        setContentView(R.layout.main);
        LoginDialog dialogFragment = LoginDialog.newInstance("My Dialog");
        dialogFragment.show(getSupportFragmentManager(), "login");
    }
}
于 2012-11-11T15:47:45.440 回答
1

也许如果您使用 fragmentransaction 代替:

public class MainActivity extends FragmentActivity
{
    ...
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager().beginTransaction(), "login");
}

编辑:

我鼓励你去看看DialogFragments上的谷歌指南。您对 alertbuilder 的自定义视图所做的操作可能会导致它不显示。

于 2012-11-11T13:13:13.240 回答
1

我想你忘了设置你的风格DialogFragment

尝试其中一种风格:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");

        // Pick a style based on the num.
        int style = DialogFragment.STYLE_NORMAL, theme = 0;
        switch ((mNum-1)%6) {
            case 1: style = DialogFragment.STYLE_NO_TITLE; break;
            case 2: style = DialogFragment.STYLE_NO_FRAME; break;
            case 3: style = DialogFragment.STYLE_NO_INPUT; break;
            case 4: style = DialogFragment.STYLE_NORMAL; break;
            case 5: style = DialogFragment.STYLE_NORMAL; break;
            case 6: style = DialogFragment.STYLE_NO_TITLE; break;
            case 7: style = DialogFragment.STYLE_NO_FRAME; break;
            case 8: style = DialogFragment.STYLE_NORMAL; break;
        }
        switch ((mNum-1)%6) {
            case 4: theme = android.R.style.Theme_Holo; break;
            case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
            case 6: theme = android.R.style.Theme_Holo_Light; break;
            case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
            case 8: theme = android.R.style.Theme_Holo_Light; break;
        }
        setStyle(style, theme);
    }

阅读更多: https ://developer.android.com/reference/android/app/DialogFragment.html

您还可以为DialogFragment设置自定义样式

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
        }
于 2017-08-04T10:05:56.043 回答
0

我的错,我在问题的顶部添加了有关导致问题的原因的信息。

编辑(@MarcinS 的评论):

public void test()
{
    DialogFragment dlg = new LoginDialog();
    dlg.show(getSupportFragmentManager(), "login");
    doThis();
    doThat();
}

该对话框不会在 dlg.show() 之后立即出现,而是在 doThat() 完成后出现。

于 2012-11-12T03:12:51.223 回答
0

我有同样的问题,在 onResume 之后没有出现对话框。(我有一个对话框通知用户他已暂停游戏并询问他是否想继续)。我只是把它提早了,我认为原因是我在 onPostResume() 中的 dialog.show() 之后加载的一个片段。我通过检测 dialog.show 是否在队列中来解决它,并且在我的片段加载后只是等到 onPostResume 才显示它。

这是显示对话框的功能:

@Override public void onShowUserDialog(String title, String message, int type) { FragmentTransaction fm = getSupportFragmentManager().beginTransaction(); Fragment prev = getSupportFragmentManager(). findFragmentByTag(PublicVar.DIALOG_USER); if (prev != null) { fm.remove(prev); fm.commit(); } if (hasPostResumed) { DialogFragment userDialog = UserDialog. newInstance(title, message, type); userDialog.show(getSupportFragmentManager().beginTransaction(), PublicVar.DIALOG_USER); dialogTitle = ""; dialogMessage = ""; dialogType = 0; } else { //saved for later dialogTitle = title; dialogMessage = message; dialogType = type; } }

hasPostResumed、dialogTitle、dialogMessage 和 dialogType 在活动中声明。

这是活动恢复时的最终调用:

protected void onPostResume() {
    super.onPostResume();
    hasPostResumed = true;

    ...function for loading my fragments...

    if (dialogType != 0) {
        onShowUserDialog(dialogTitle, dialogMessage, dialogType);
    }
}
于 2014-05-29T07:09:04.433 回答
0

FragmentTransaction.commit()(它在你显示时被调用DialogFragment)不会立即提交,而是在主线程准备好时。由于您希望立即完成,因此必须getSupportFragmentManager().executePendingTransactions()在显示对话框后添加。

来源:FragmentTransaction.commit()FragmentManager.executePendingTransactions()

于 2015-08-28T11:34:30.157 回答