如何在“确定”按钮点击后取消偏好设置对话框的关闭?

3

我该如何在点击任何按钮后取消首选项对话框的关闭?

我解决了这个问题,使我的对话框类实现了OnClickListener接口。

public class PassChangeDiglog extends DialogPreference implements
        OnClickListener {
    EditText oldpass, new_pass1, new_pass2;

    public PassChangeDiglog(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.pass_change_diglog);
        setPositiveButtonText("OK");
        setNegativeButtonText(R.string.Cancel);

    }


@Override
protected void onBindDialogView(View view) {
    setTitle(R.string.PassChanging);
    oldpass = (EditText) view.findViewById(R.id.Dlg_old_pass);
    new_pass1 = (EditText) view.findViewById(R.id.Dlg_NewPass1);
    new_pass2 = (EditText) view.findViewById(R.id.Dlg_NewPass2);

    super.onBindDialogView(view);
}


    @Override
    public void onClick(DialogInterface arg0, int arg1) {
        switch (arg1) {
        case DialogInterface.BUTTON_POSITIVE:
           boolean needclose;
           ...
           if (needclose)
               arg0.dismiss();
           else{
               //do not close
           }
        }

    };
}

我尝试重写onDismiss方法,但是对话框无论如何都不会关闭。

@Override
public void onDismiss(DialogInterface dialog) {
    // TODO Auto-generated method stub
    Log.d("MY","onDismiss");
    //super.onDismiss(dialog);
}
2个回答

3
我的问题的解决方案是覆盖showDialog方法。
@Override
protected void showDialog(Bundle state) {
    // TODO Auto-generated method stub
    super.showDialog(state);
    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE)
            .setOnClickListener(this);
}

谢谢,这正是我需要的。 - Pavel Shorokhov

0

您需要在对话框显示后添加其他监听器after

    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
          new View.OnClickListener()
          {            
              @Override
              public void onClick(View view)
              {
                 // do your magic
              }
          });

为了让它与Preference一起工作,请使用AlertDialog的cast: (AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)

我已添加了一个类的描述。 不幸的是,这个方法不适用于我的类。 我尝试过从 this、从 this.getDialog() - Vladislav
我的应用程序在这行代码之后崩溃了: ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 或者在调用任何方法之后... - Vladislav

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