onClickListener内的AlertDialog [英] AlertDialog inside onClickListener

查看:95
本文介绍了onClickListener内的AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从onClickListener启动AlertDialog,但出现以下错误.

I'm trying to start an AlertDialog from an onClickListener but I'm getting the following error.

The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined  

有人知道如何解决此问题吗?

Does anyone know how to fix this?

        mRecordButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new AlertDialog.Builder( this )
            .setTitle( "Cast Recording" )
            .setMessage( "Now recording your message" )
            .setPositiveButton( "Save", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Positive" );
                }
            })
            .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    Log.d( "AlertDialog", "Negative" );
                }
            } )
            .show();
        }
    });

推荐答案

更改此行

new AlertDialog.Builder( this );

new AlertDialog.Builder( YourActivity.this );

这是因为构造函数需要上下文类型& OnclickListner is not a Context type,因此您可以使用Activity的对象.

This is because the constructor needs a Context type & OnclickListner is not a Context type so you use the object of your Activity.

希望对您有帮助.

这篇关于onClickListener内的AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆