DialogFragment不显示DialogFragment

2023-09-07 10:13:10 作者:陌上花开迟

在最后,我发现这个问题是什么...

如果你有这样的功能:

 公共无效测试(){    DialogFragment DLG =新一个LoginDialog();    dlg.show(getSupportFragmentManager(),登录);} 

当测试()完成该对话框才会出现,我不知道,如果这是唯一的办法Android的对话框的作品,但我一定会读到更多关于这个...

原题:

我是新到Android的世界,有人可以提供一些线索?

dlg.show()无一例外地执行,但只是什么也没有发生,我应该怎么做才能知道什么是错?该项目采用Android 2.2的API。

 公共类MainActivity扩展FragmentActivity{    ...    DialogFragment DLG =新一个LoginDialog();    dlg.show(getSupportFragmentManager(),登录);} 
DialogFragment浅析

对话框布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:方向=垂直机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT><的EditText    机器人:ID =@ + ID /用户名    安卓的inputType =textEmailAddress    机器人:layout_width =match_parent    机器人:layout_height =WRAP_CONTENT    机器人:layout_marginTop =16DP    机器人:layout_marginLeft =4DP    机器人:layout_marginRight =4DP    机器人:layout_marginBottom =4DP    机器人:提示=用户名/><的EditText    机器人:ID =@ + ID /密码    安卓的inputType =textPassword    机器人:layout_width =match_parent    机器人:layout_height =WRAP_CONTENT    机器人:layout_marginTop =4DP    机器人:layout_marginLeft =4DP    机器人:layout_marginRight =4DP    机器人:layout_marginBottom =16DP    机器人:提示=密码/>< / LinearLayout中> 

对话框类:

 进口android.app.AlertDialog;进口android.app.Dialog;进口android.content.DialogInterface;进口android.os.Bundle;进口android.support.v4.app.DialogFragment;进口android.view.LayoutInflater;公共类扩展的LoginDialog DialogFragment{    @覆盖    公共对话框onCreateDialog(捆绑savedInstanceState)    {        AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());        //获取吹气布局        LayoutInflater吹气= getActivity()getLayoutInflater()。        //充气并为对话设置布局        作为父视图//传递空值,因为它在该对话框的布局会        builder.setView(inflater.inflate(R.layout.login_dialog,NULL))        //添加动作按钮               .setPositiveButton(登录,新DialogInterface.OnClickListener(){                   公共无效的onClick(DialogInterface对话,诠释的id){                       // SIGN在用户...                   }               })               .setNegativeButton(取消,新DialogInterface.OnClickListener(){                   公共无效的onClick(DialogInterface对话,诠释的id){                        // 做一点事                   }               });        返回builder.create();    }} 

解决方案

什么的问题上造成的问题我不好,我已经添加信息。

Edit(对于@MarcinS'评论)

 公共无效测试(){    DialogFragment DLG =新一个LoginDialog();    dlg.show(getSupportFragmentManager(),登录);    做这个();    去做();} 

该对话框不dlg.show()后立即出现,找时间做()结束后出现。

At the last, I found what the problem is...

If you have a function like this:

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

The dialog will only appear when test() finished, I'm not sure if this is the only way Android dialog works but I'll definitely read more on this...

Original Question:

I'm new to android world, can somebody shed some light?

dlg.show() executed without exception but just nothing happens, what should I do to know what's wrong? The project is using Android 2.2's API.

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

The dialog layout:

<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>

The dialog class:

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();
    }
}

解决方案

My bad, I've added information about what caused the issue at the top of the question.

Edit (for @MarcinS' comment):

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

The dialog does not appear immediately after dlg.show(), it appears after doThat() has finished.