Hide buttons in DialogFragment in Android

。_饼干妹妹 提交于 2019-12-12 00:30:14

问题


I have Dialog Fragment implemented like this

public class SessionExpiredFragment extends DialogFragment {

    public interface SessionExpiredFragmentListener {
        public void onCancelLoginProcessPressed(DialogFragment dialog);

        // validValues = true if fields are not empty and email is a valid
        // email,
        // else validValues = false;
        public void onOKLoginProcessPressed(DialogFragment dialog,
                boolean validValues);
    }

    SessionExpiredFragmentListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (SessionExpiredFragmentListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement SessionExpiredFragmentListener");
        }
    }

    // UI references
    private EditText mEmailView;
    private EditText mPasswordView;
    private View mLoginFormView;
    private View mLoginStatusView;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        View view = inflater.inflate(R.layout.login_fragment_layout, null);
        /*
         * Get edit texts references
         */
        mEmailView = (EditText) view.findViewById(R.id.email);
        mPasswordView = (EditText) view.findViewById(R.id.password);
        mLoginFormView = view.findViewById(R.id.login_form);
        mLoginStatusView = view.findViewById(R.id.login_status);

        /*
         * Set builder values
         */
        builder.setMessage(R.string.session_expired_title)
                .setView(view)
                .setPositiveButton(R.string.action_ok,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                boolean validValues = true;
                                String email = mEmailView.getText().toString();
                                String password = mPasswordView.getText()
                                        .toString();
                                if (TextUtils.isEmpty(email)
                                        || TextUtils.isEmpty(password))
                                    validValues = false;
                                if (!isValidEmail(email))
                                    validValues = false;

                                mListener.onOKLoginProcessPressed(
                                        SessionExpiredFragment.this,
                                        validValues);
                            }
                        })
                .setNegativeButton(R.string.action_cancel,
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                mListener
                                        .onCancelLoginProcessPressed(SessionExpiredFragment.this);

                            }
                        });
        return builder.create();
    }

and it has Positive and Negative buttons. What I need to do is to hide them when Positive button is pressed. I use this Listener, so I can listen in my activity that, but that didn't help me either. HOW (and WHERE to add that code) to hide buttons? Thanks for the help.


回答1:


Button negativeButton = alert.getButton(AlertDialog.BUTTON_NEGATIVE);
buttonNo.setEnabled(false);



回答2:


Then you should consider creating custom view and manage visibility of buttons by code




回答3:


Just override onStart() in DialogFragment and keep a reference to the button. You can use the reference to re-enable the button later:

Button positiveButton;

@Override
public void onStart() {
    super.onStart();
    AlertDialog d = (AlertDialog) getDialog();
    if (d != null) {
        positiveButton = (Button)d.getButton(Dialog.BUTTON_POSITIVE);
        positiveButton.setEnabled(false);
    }

}


来源:https://stackoverflow.com/questions/18465441/hide-buttons-in-dialogfragment-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!