如果选择“是”,我有一个启动视频的对话框。视频启动并播放,但一旦完成,对话框仍然存在

时间:2012-07-20 11:33:13

标签: android video dialog

我正在编写一个Android程序来显示一个对话框,并根据在对话框中单击的按钮显示或不显示视频。当用户单击是按钮时,它将播放。但是当它结束时,对话框仍然存在。为什么呢?

对话框的代码

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //builder.setTitle(" MY_TEXT");
    builder.setMessage("Would you like to see a video?")
    .setView(checkBoxView)
    .setCancelable(false)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //Start the movie
            startVideo();
            dialog.cancel();
        }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    }).show().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

视频代码

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    final VideoView videoHolder = new VideoView(this);
    //if you want the controls to appear
    videoHolder.setMediaController(new MediaController(this));
    Uri video = Uri.parse("android.resource://" + getPackageName() + "/" 
            + R.raw.documentariesandyou); //do not add any extension
    //if your file is named sherif.mp4 and placed in /raw
    //use R.raw.sherif
    videoHolder.setVideoURI(video);
    setContentView(videoHolder);
    videoHolder.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            //exits video!
            videoHolder.stopPlayback();
            mp.release();
            finish();
        }
    });
    videoHolder.start();

2 个答案:

答案 0 :(得分:0)

而不是dialog.cancel()

使用

dismissDialog();

dismissDialog()将包含自我名称作为参数。 检查一下。

答案 1 :(得分:0)

尝试使用dialog.dismiss()代替dialog.cancel();