最后,我发现了问题所在...
如果你有这样的功能:
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();
}
}