0

我有一个播放 gif 动画的对话框。我想在特定时间后停止动画并返回第二个活动。但是当我完成 gif 动画时,活动将回到我不想要的第一个活动。这是我尝试过的代码。

        ImageButton apple = (ImageButton)findViewById(R.id.imageButton1);
    apple.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder builder = new AlertDialog.Builder(Fruits.this);
            WebView view = new WebView(Fruits.this);
            view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            builder.setView(view);
            builder.create().show();
            view.loadUrl("file:///android_asset/apple.gif");

            gifIsPlaying = true;

            new Handler().postDelayed(new Runnable() {
                public void run() {
                    gifIsPlaying = false;
                    finish();
                }
            }, PLAYING_TIME_OF_GIF);

        }

    });
4

1 回答 1

1

这是因为finish()完成了Activity. 你应该dismiss()这样Dialog

ImageButton apple = (ImageButton) findViewById(R.id.imageButton1);
apple.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        AlertDialog.Builder builder = new AlertDialog.Builder(Fruits.this);
        WebView view = new WebView(Fruits.this);
        view.setLayoutParams(
                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        builder.setView(view);
        final Dialog d = builder.create(); // save a Dialog reference here
        d.show();
        view.loadUrl("file:///android_asset/apple.gif");

        gifIsPlaying = true;

        new Handler().postDelayed(new Runnable() {
            public void run() {
                gifIsPlaying = false;
                if (d.isShowing()) { // dismiss dialog if still showing
                    d.dismiss();
                }
            }
        }, PLAYING_TIME_OF_GIF);

    }
});
于 2013-10-30T07:47:42.500 回答