防止 DialogFragment 在按钮点击时消失

46

我有一个带有自定义视图的DialogFragment,其中包含两个文本字段,用户需要在这里输入他们的用户名和密码。当点击确定按钮时,我希望验证用户在关闭对话框之前实际上输入了一些内容。

public class AuthenticationDialog extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.authentication_dialog, null))
            .setPositiveButton(getResources().getString(R.string.login), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            })
            .setNegativeButton(getResources().getString(R.string.reset), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO
                }
            });

        return builder.create();
    }
}

那么我该如何防止对话框消失?我需要覆盖哪个方法吗?


如何防止对话框消失? - 不要使用对话框的默认“按钮”(使用setPositiveButton等设置的按钮)。为了关闭对话框,请设置自己的“按钮”,并在它们的“OnClickListeners”中实现所需的逻辑。您应该发布您的“DialogFragment”的代码。 - user
@Luksprog 代码已发布。当我尝试您建议的方法时,从onCreateDialog中获取自定义视图元素的引用始终返回null。 - Groppe
此时对话框还没有在屏幕上渲染。相反,尝试将布局R.layout.authentication_dialog填充到一个View引用中,然后搜索ButtonsView v = inflater.inflate(R.layout.authentication_dialog); Button b = (Button) v.findviewById(R.id.the_btn_id); - user
上面的注释中的代码没有起作用? - user
是的,我刚刚发布了我的整个对话框类,供任何人使用。 - Groppe
显示剩余2条评论
4个回答

74
在OnStart()中重写默认按钮处理程序来实现这一点。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage("Test for preventing dialog close");
    builder.setPositiveButton("Test", 
        new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                //Do nothing here because we override this button later to change the close behaviour. 
                //However, we still need this because on older versions of Android unless we 
                //pass a handler the button doesn't get instantiated
            }
        });
    return builder.create();
}

@Override
public void onStart()
{
    super.onStart();    //super.onStart() is where dialog.show() is actually called on the underlying dialog, so we have to do it after this point
    AlertDialog d = (AlertDialog)getDialog();
    if(d != null)
    {
        Button positiveButton = (Button) d.getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        Boolean wantToCloseDialog = false;
                        //Do stuff, possibly set wantToCloseDialog to true then...
                        if(wantToCloseDialog)
                            dismiss();
                        //else dialog stays open. Make sure you have an obvious way to close the dialog especially if you set cancellable to false.
                    }
                });
    }
}

请查看我在这里的答案https://dev59.com/lXE85IYBdhLWcg3wwWet#15619098,了解其他对话框类型的更多说明和示例。


1
这对我不起作用。d.getButton(Dialog.BUTTON_POSITIVE) 返回 null。 - Travis Christian
1
@Travis Christian,你可能在针对较旧版本的Android进行开发。我之前也遇到过这个问题,我已经更新了我的答案,现在它可以在旧版本上运行。你需要将一个空的点击监听器传递给第一个调用。 - Sogger
1
有趣,谢谢。我的目标是15岁,所以这一定是最近的变化。 - Travis Christian
1
我猜只是因为新旧版本的问题,因为它在一个较新的手机上(我想是ICS)可以工作,但在一个旧的Gigngerbread手机上没有定义空监听器就不能工作。不过,自从添加了空监听器后,它在两个手机上都可以工作了。 - Sogger
它有所帮助,非常感谢。但是很奇怪,这不是一个bug吗? - trickbz

12

这是Karakuri和Sogger答案的“最佳解决方案”。Karakuri走在了正确的轨道上,但是如果按钮没有显示(如评论中所述),则只能以这种方式获得按钮(否则为null)。这就是为什么Sogger的答案有效,但我更喜欢相同方法的设置,即onCreateDialog,而不是另外在onStart中。解决方案是将获取按钮的操作包装到对话框的OnShowListener中。

public Dialog onCreateDialog(Bundle savedInstanceState) {
  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  // your dialog setup, just leave the OnClick-listeners empty here and use the ones below

  final AlertDialog dialog = builder.create();
  dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(final DialogInterface dialog) {
      Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
      positiveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
          // TODO - call 'dismiss()' only if you need it
        }
      });
      Button negativeButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
      // same for negative (and/or neutral) button if required
    }
  });

  return dialog;
}

抱歉我给你的投票打了个负分,但是这个方法在我的Lollipop系统上没有触发setOnShowListener中定义的新onClick事件。你测试过吗? - Abraham Philip
当然,我多次测试和使用了这个功能,而且很明显不仅仅对我有效。在你给一个几个月前的回答点踩之前,或许你应该考虑一下这是在Lollipop发布之前发布的事实... - Blacklight
嗯,有趣的观点。看了http://meta.stackoverflow.com/q/265749/3000919和http://meta.stackoverflow.com/q/265749/3000919两个链接后,共识似乎是这样的答案应该被编辑,注明其已过时的性质,但为了支持旧系统而保留不变。然而,在这种情况下,由于排名最高的答案在所有版本上都有效(截至目前),因此此答案作为“遗留”解决方案的实用性并不清楚。如果您在顶部添加免责声明,说明此解决方案在较新版本的Android上无效,我将很乐意撤销我的投票,以便不会拒绝您(继续) - Abraham Philip
(续上一条评论)感谢您在当时提供了有效的答案,这是您应得的赞扬。另外,我似乎在之前的评论中贴了两次同样的链接。我指的是这个问题:http://meta.stackexchange.com/q/11705 - Abraham Philip
@AbrahamPhilip 为了能够验证或编辑我的答案,我专门建立了一个测试项目。我的解决方案仍然有效,即使在最新的SDK(API级别22)上,这个答案仍然是正确的。请检查您的解决方案,因为显然您正在做一些错误的事情。 - Blacklight

6

感谢Luksprog, 我找到了解决方案。

AuthenticationDialog.java:

public class AuthenticationDialog extends DialogFragment implements OnClickListener {

    public interface AuthenticationDialogListener {
        void onAuthenticationLoginClicked(String username, String password);
        void onAuthenticationResetClicked(String username);
    }

    private AuthenticationDialogListener mListener;

    private EditText mUsername;
    private EditText mPassword;
    private Button mReset;
    private Button mLogin;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.authentication_dialog, container);
        this.getDialog().setTitle(R.string.login_title);

        mUsername = (EditText) view.findViewById(R.id.username_field);
        mPassword = (EditText) view.findViewById(R.id.password_field);
        mReset = (Button) view.findViewById(R.id.reset_button);
        mLogin = (Button) view.findViewById(R.id.login_button);

        mReset.setOnClickListener(this);
        mLogin.setOnClickListener(this);

        return view;
    }

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (AuthenticationDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement AuthenticationDialogListener");
        }
    }

    public void onClick(View v) {
        if (v.equals(mLogin)) {
            if (mUsername.getText().toString().length() < 1 || !mUsername.getText().toString().contains("@")) {
                Toast.makeText(getActivity(), R.string.invalid_email, Toast.LENGTH_SHORT).show();
                return;
            } else if (mPassword.getText().toString().length() < 1) {
                Toast.makeText(getActivity(), R.string.invalid_password, Toast.LENGTH_SHORT).show();
                return;
            } else {
                mListener.onAuthenticationLoginClicked(mUsername.getText().toString(), mPassword.getText().toString());
                this.dismiss();
            }
        } else if (v.equals(mReset)) {
            mListener.onAuthenticationResetClicked(mUsername.getText().toString());
        }
    }
}

authentication_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/username_field"
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:layout_marginBottom="4dp"
        android:hint="@string/username"
        />
    <EditText
        android:id="@+id/password_field"
        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="12dp"
        android:fontFamily="sans-serif"
        android:hint="@string/password"
        />
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/dividerVertical" 
        />
    <LinearLayout 
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="0dp"
        android:measureWithLargestChild="true" >
        <Button 
            android:id="@+id/reset_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1.0"
            android:text="@string/reset"
            />
        <Button 
            android:id="@+id/login_button"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1.0"
            android:text="@string/login"
            />
    </LinearLayout>
</LinearLayout>

-2

你可以再次弹出对话框。或者,你可以在两个字段都有输入之前保持积极按钮禁用。如果你是在onCreateView()中创建布局,这很容易实现。如果你使用AlertDialog.Builder类,你可以像这样获取按钮的句柄:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
/* ... */
Dialog dialog = builder.create();
Button positiveButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
/* now you can affect the button */

1
由于某些原因,如果我在调用builder.create()之后但在显示对话框之前尝试调用getButton,则会返回null。 - Groppe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接