gpt4 book ai didi

android - onClickListener 中的 AlertDialog

转载 作者:可可西里 更新时间:2023-11-01 19:12:04 27 4
gpt4 key购买 nike

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

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

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

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

这是因为构造函数需要一个 Context 类型 & OnclickListner 不是 Context 类型 所以你使用你的 Activity 的对象。

希望对你有帮助

关于android - onClickListener 中的 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8813565/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com