如何让DialogPreference POSITIVE_BUTTON在OnClick上运行?

San*_*ers 2 java mobile events android onclick

我正在尝试写一些东西来设置密码DialogPrefence.如何从对话框的OK按钮获取onClick()事件?

这是代码:

package com.kontrol.app;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.util.AttributeSet;

public class SS1_Senha extends DialogPreference implements DialogInterface.OnClickListener{

    public SS1_Senha(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        setDialogLayoutResource(R.layout.ss1_senha);

        setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //Action after OK

            }
        });


    }
}
Run Code Online (Sandbox Code Playgroud)

Lib*_*bin 13

您需要实现DialogInterface.OnClickListener并处理OnClick每个按钮的事件

DialogPreference像这样创建一个自定义类

public class CustomDialogPreference extends DialogPreference implements DialogInterface.OnClickListener{

public CustomDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
    setPersistent(false);
    setDialogLayoutResource(R.layout.image_dialog);
    setPositiveButtonText("OK");
    setNegativeButtonText("CANCEL");
}

@Override
public void onClick(DialogInterface dialog, int which){
    if(which == DialogInterface.BUTTON_POSITIVE) {
        // do your stuff to handle positive button
    }else if(which == DialogInterface.BUTTON_NEGATIVE){
        // do your stuff to handle negative button
    }
 }
}
Run Code Online (Sandbox Code Playgroud)