java - 即使设置可取消为 true 后,自定义对话框也不会取消

标签 java android eclipse android-studio

我有一个自定义的 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();
    }
}

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

        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();

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

最佳答案

不要重写 onCreate()。您调用的 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);
    }
}

关于java - 即使设置可取消为 true 后,自定义对话框也不会取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31130744/

相关文章:

java - 文件的递归反向行不适合我

android - 退出Activity后如何释放实际的Activity对象内存?

android - "Eclipse"故障信息错误不断出现

java - 仅对编辑的代码应用格式化程序 - eclipse

java - 我可以用 Java 将文件放入 LInux 垃圾箱吗

java - 没有任何命名库的 native 中的 JVM SIGSEGV 崩溃

java - 在 Android 中调整和旋转图像

python - libjpeg.so.62 : cannot open shared object file: No such file or directory

java - 为什么java语言禁止程序在程序迭代集合时更新它?

android - 你如何从 NDK 中的 SurfaceTexture 获取 ANativeWindow