【问题标题】:Android DialogFragment done button clickableAndroid DialogFragment 完成按钮可点击
【发布时间】:2014-09-27 15:02:24
【问题描述】:

我有一个带有编辑文本(自定义布局)的对话框片段。我想让“完成”按钮仅在编辑文本不为空时才可点击。当对话框打开时,它应该是不可点击的。

这是我的 DialogFragment 类:

public class AddNumberDialog extends DialogFragment {

private Context mContext;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mContext = activity;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View mView = inflater.inflate(R.layout.dialog_add_number, null);

    final EditText mEditText = (EditText)
            mView.findViewById(R.id.number_edit_text);

    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(mView)
            .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    String number = mEditText.getText().toString();
                    //Add number to list
                }
            })
            .setNegativeButton(R.string.dialog_cancel, null);

    return builder.create();
}

}

【问题讨论】:

    标签: android button dialog android-edittext android-dialogfragment


    【解决方案1】:

    你必须使用对话框。因为警报对话框按钮会在按下时关闭警报对话框。无需执行 OnClickListener 中的代码。 您可以阻止 AlertDialog 关闭。见this Answer if someone is interested

    重写 onStart 方法以设置Done Button Clickable False.

    public class AddNumberDialog extends DialogFragment {
    
            private Context mContext;
    
            @Override
            public void onAttach(Activity activity) {
               super.onAttach(activity);
               mContext = activity;
            }
    
    
        Button button; // Your done Button
    
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View mView = inflater.inflate(R.layout.dialog_add_number, null);
    
            final EditText mEditText = (EditText)
                    mView.findViewById(R.id.number_edit_text);
    
            mEditText.addTextChangedListener(new TextWatcher(){
                  public void afterTextChanged(Editable s) {
                        if(s.length()>0){
                              button.setEnabled(true);
                        }else{
                              button.setEnabled(false);
                        }
                  }
                  public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                  public void onTextChanged(CharSequence s, int start, int before, int count){}
            }); 
    
    
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setView(mView)
                    .setPositiveButton(R.string.dialog_done, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // Do Nothing here... We Will be getting the Button Click Listener in Onstart
                        }
                    })
                    .setNegativeButton(R.string.dialog_cancel, null);
    
            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= (Button) d.getButton(Dialog.BUTTON_NEGATIVE);
                button.setOnClickListener(new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v)
                            {
    
                                String number = mEditText.getText().toString();
                                //Add number to list
    
                                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.
                            }
                        });
            }
        }
    }
    

    【讨论】:

    • 谢谢!我在 onStart() 中做了这个:
    • 按钮 mButton = ((AlertDialog) getDialog()).getButton(Dialog.BUTTON_POSITIVE); mButton.setEnabled(false);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-26
    • 2019-04-21
    相关资源
    最近更新 更多