android中AlertDialog按钮的问题

发布于 2024-11-29 08:54:20 字数 1190 浏览 0 评论 0原文

我有一个警报对话框,应该将布尔值设置为 true。对于 setPositiveButton 我将 Dialog onclick 界面设置为 null。当我将 onClickListener 添加到 setNegativeButtons onclick 界面时,它给我一个编译错误:方法 setNegativeButton(int, DialogInterface. AlertDialog.Builder 类型中的 OnClickListener) 不适用于参数 (String, new View.OnClickListener(){})

这是我的代码,为什么我会收到编译错误以及如何修复此错误?谢谢

new AlertDialog.Builder(ImTracking.this)
    .setMessage(
            Html.fromHtml(getText("http://www.cellphonesolutions.net/im-following-"
                    + getResources().getString(
                            R.string.Country))))
    .setPositiveButton("OK", null)
    // The red squigly is under the .setNegativeButton
    .setNegativeButton("Don't Remind", new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            SharedPreferences prefs= getSharedPreferences("Settings",0);
            SharedPreferences.Editor editor=prefs.edit();
            editor.putBoolean("ImTrackingDontRemind",true);
            editor.commit();
        }
    }).show();

I have an Alert Dialog that is supposed to set a boolean to true. For setPositiveButton I have the Dialog onclick interface as null. When I add an onClickListener to the setNegativeButtons onclick interface it gives me a compile error saying: The method setNegativeButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder is not applicable for the arguments (String, new View.OnClickListener(){})

Here is my code, why am I getting a compile error and how do I fix this? Thanks

new AlertDialog.Builder(ImTracking.this)
    .setMessage(
            Html.fromHtml(getText("http://www.cellphonesolutions.net/im-following-"
                    + getResources().getString(
                            R.string.Country))))
    .setPositiveButton("OK", null)
    // The red squigly is under the .setNegativeButton
    .setNegativeButton("Don't Remind", new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            SharedPreferences prefs= getSharedPreferences("Settings",0);
            SharedPreferences.Editor editor=prefs.edit();
            editor.putBoolean("ImTrackingDontRemind",true);
            editor.commit();
        }
    }).show();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-12-06 08:54:20

所以这是应该的

  alertDialog.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {

                        // TODO Auto-generated method stub

                        SharedPreferences prefs= getSharedPreferences("Settings",0);
                        SharedPreferences.Editor editor=prefs.edit();
                        editor.putBoolean("ImTrackingDontRemind",true);
                        editor.commit();

    } });

So this is was it should be

  alertDialog.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {

                        // TODO Auto-generated method stub

                        SharedPreferences prefs= getSharedPreferences("Settings",0);
                        SharedPreferences.Editor editor=prefs.edit();
                        editor.putBoolean("ImTrackingDontRemind",true);
                        editor.commit();

    } });
同尘 2024-12-06 08:54:20

这是你的解决方案,你犯了一个愚蠢的错误,伙计。

不应该是

.setNegativeButton("Don't Remind", new OnClickListener() 

应该是

.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener()

Here is your solution, you did a silly mistake there buddy.

It should not be

.setNegativeButton("Don't Remind", new OnClickListener() 

It should be

.setNegativeButton("Don't Remind", new DialogInterface.OnClickListener()
背叛残局 2024-12-06 08:54:20

除了提供答案之外,也许谨慎的做法是解释为什么会出现这个答案。另外,肖恩的问题是,

...为什么会出现编译错误以及如何解决此问题?

强调我的。虽然接受的答案回答了后一个问题,但它并不试图回答前一个问题。

Sean,您正在创建的 onClickListner 匿名内部类实际上是 View 的成员函数,因为您没有提供类名。您的编译错误源于以下事实: AlertDialog 扩展了 Dialog 类,而不是 View 类,因此具有 onClickListner > 具有不同函数签名的成员函数:

public abstract void onClick (DialogInterfacedialog, int which)

ViewonClickListner 成员函数相比, :

public Abstract void onClick (查看v)

In addition to providing the answer, perhaps it would also be prudent to provide an explanation of why that's the answer. Plus, Sean's question was,

...why am I getting a compile error and how do I fix this?

emphasis mine. While the accepted answer answers the latter question, it does not attempt to answer the former question.

Sean, the onClickListner anonymous inner class you're creating is actually a member function of View since you provided no class name. Your compile error stems from the fact that AlertDialog extends the Dialog class, not the View class and thus has an onClickListner member function with a different function signature:

public abstract void onClick (DialogInterface dialog, int which)

than that of View's onClickListner member function:

public abstract void onClick (View v).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文