【问题标题】:Custom Dialog not cancelling even after setting cancellable true即使设置cancellable true后自定义对话框也不会取消
【发布时间】:2015-06-30 06:05:35
【问题描述】:

我有一个自定义的 ActivityIndi​​cator 定义为这个

public class ActivityIndicator extends Dialog
{
    private ImageView progress;
    private ImageView bottomProgress;

    private int type = INDICATOR_SIMPLE;

    public static final int INDICATOR_SIMPLE = 0;
    public static final int INDICATOR_BOTTOM = 1;

    public ActivityIndicator(Context context, int theme, int type)
    {
        super(context, theme);
        this.type = type;
        onCreate(null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_indicator);

        progress = (ImageView) findViewById(R.id.progress);
        bottomProgress = (ImageView) findViewById(R.id.bottomProgress);

        if(type == INDICATOR_BOTTOM)
        {
            progress.setVisibility(View.INVISIBLE);
        }
        else if(type == INDICATOR_SIMPLE)
        {
            bottomProgress.setVisibility(View.INVISIBLE);
        }
        this.setCancelable(false);
    }

    @Override
    public void show()
    {
        progress.clearAnimation();
        bottomProgress.clearAnimation();

        if(type == INDICATOR_BOTTOM)
        {
            progress.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
                    bottomProgress.startAnimation(anim);
                }
            },400);
        }

        if(type == INDICATOR_SIMPLE)
        {
            bottomProgress.setVisibility(View.INVISIBLE);
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotating_img);
                    progress.startAnimation(anim);
                }
            },400);
        }
        super.show();
    }

    @Override
    public void dismiss()
    {
        super.dismiss();
        progress.clearAnimation();
        bottomProgress.clearAnimation();
    }
}

在我的活动中,我将其初始化为:

        indicator = new ActivityIndicator(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen, ActivityIndicator.INDICATOR_SIMPLE);

现在如代码所示,可取消的默认样式为 false。

但是在某些时候我确实想把它取消,这是我的代码:

        indicator.setCancelable(true);
        indicator.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialog)
            {
                finish();
            }
        });
indicator.show();

当我尝试按下后退按钮时,没有任何反应,dialog 不会取消,也不会取消侦听器。这里有什么问题?为什么按下后退键不自动取消

【问题讨论】:

  • 代替finish() 试试dismiss()dialog.dismiss()
  • @Clairvoyant 这就是重点.. 在哪里调用这些 :) 取消监听器不起作用。后按什么都不做
  • 抱歉,我没有完整阅读您的问题
  • 您不应该破解代码来解决问题。您的问题在于 onCreate。看看我的回答。

标签: java android eclipse android-studio


【解决方案1】:

不要OverrideonCreate()。您调用的 onCreate(null) 方法正在搞砸您的代码。而是使用初始化模式来初始化Dialog

如果您将 onCreate 更改为 initialize() 并从构造函数调用它,代码将起作用。

看下面。

public ActivityIndicator(Context context, int theme, int type)
{
    super(context, theme);
    this.type = type;

    initialize();
}

protected void initialize()
{
    setContentView(R.layout.dialog_indicator);
    setCancelable(false);

    progress = (ImageView) findViewById(R.id.progress);
    bottomProgress = (ImageView) findViewById(R.id.bottomProgress);

    if(type == INDICATOR_BOTTOM)
    {
        progress.setVisibility(View.INVISIBLE);
    }
    else if(type == INDICATOR_SIMPLE)
    {
        bottomProgress.setVisibility(View.INVISIBLE);
    }
}

【讨论】:

    【解决方案2】:

    请评论您的 seton cancellabel 并使用下面的代码并检查。

    indicator.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK){
                finish();
            }
        }
    }
    

    【讨论】:

    • 好的,这是一个很好的答案,但是它仍然不能回答我的问题,为什么会出现这种行为?
    • 这不是一个可行的解决方案。它可能会解决问题,但它是一个 hack。
    【解决方案3】:

    当您创建ActivityIndicator 的实例时,在OnCreate 方法中,setCancelable 设置为false。

    尝试删除它..

    【讨论】:

    • 但他在创建对象时将其设置为true。
    【解决方案4】:

    如果你遇到问题,只需像下面这样更改你的构造函数,你就会调用你的取消侦听器:

    public ActivityIndicator(Context context, int theme, int type, boolean isCancelable)
        {
            super(context, theme);
            this.type = type;
            onCreate(null);
            this.setCancelable(isCancelable); //setcancelable here on the basis of boolean value and remove setcancelable from onCreate()
        }
    

    用另外一个布尔值 true/false 参数调用构造函数

    注意:不要忘记从 onCreate() 方法中删除 setCancelable()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多