【问题标题】:How to get the DialogPreference POSITIVE_BUTTON to work on OnClick?如何让 DialogPreference POSITIVE_BUTTON 在 OnClick 上工作?
【发布时间】:2014-04-22 02:25:48
【问题描述】:

我正在尝试写一些东西来设置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

            }
        });


    }
}

【问题讨论】:

    标签: java android events mobile onclick


    【解决方案1】:

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

    【讨论】:

    • setNegativeButtonText("CANCEL");
    【解决方案2】:

    如果我理解正确,您需要了解其他类(而不是 SS1_Senha)中的按键事件。为此,您可以使用侦听器(观察者)模式或处理程序。

    【讨论】:

      【解决方案3】:

      要显示警报对话框,您可以使用 AlertDialog.builder。 例如:

      AlertDialog alertDialog = new AlertDialog.Builder(
                              AlertDialogActivity.this).create();
      
          // Setting Dialog Title
          alertDialog.setTitle("Alert Dialog");
      
          // Setting Dialog Message
          alertDialog.setMessage("My Message");
      
      
          // Setting OK Button
          alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                  // Write your code here to execute after dialog closed
                  Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
                  }
          });
      
          // Showing Alert Message
          alertDialog.show();
      

      【讨论】:

        猜你喜欢
        • 2014-10-05
        • 1970-01-01
        • 2012-09-22
        • 1970-01-01
        • 1970-01-01
        • 2019-12-02
        • 2013-09-15
        • 2013-05-19
        • 2016-09-19
        相关资源
        最近更新 更多