java - 如何让 DialogPreference POSITIVE_BUTTON 在 OnClick 上工作?

标签 java android events mobile 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

            }
        });


    }
}

最佳答案

您需要实现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
    }
 }
}

关于java - 如何让 DialogPreference POSITIVE_BUTTON 在 OnClick 上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209311/

相关文章:

java - 为什么菜单没有被添加到 Android 的工具栏中?

android - 我应该使用哪种服务器型号? SQlite 还是 MySql?

javascript - 事件不适用于使用 Snap.svg 制作的 SVG

c# - c# 中的事件处理程序 - 语法/模式

java.net 源代码阅读器跳行

java - 通过使用 Jackson API 动态发送 key 来检索 JSON 字符串中的值?

java - 这里的运行时类型对象是什么

java - ADF - 如何取消命令按钮的操作?

android - FCM接收消息问题

c++ - SDL 不使用事件获取键盘状态